I'm not certain if I'm missing something completely obvious, but every time I call upon unittest.main() my commandline output reads out:
Ran 0 tests in 0.000s
It should say that I've run two tests, though, given the code here:
import unittest
from Chap11Lesson2 import Employee
class EmployeeTest(unittest.TestCase):
def setUp(self):
"""Setting up a variable for use in the test methods"""
self.employee1 = Employee("Lucas", "Grillos", 20000)
def give_default_raise(self):
"""Testing to see if 5000 is added properly"""
money = 25000
self.employee1.give_raise()
self.assertEqual(self.employee1.salary, money)
def give_custom_raise(self):
"""Testing to see if 10000 is raised properly"""
money = 35000
self.employee1.give_raise(10000)
self.assertEqual(self.employee1.salary, money)
unittest.main()
Here's the class that it's testing from:
class Employee():
def __init__(self, first_name, last_name, salary):
self.first_name = first_name
self.last_name = last_name
self.salary = salary
def give_raise(self, salary_raise = None):
if salary_raise:
self.salary = self.salary + salary_raise
else:
self.salary = self.salary + 5000
def print_salary(self):
print(self.salary)
I've never had a problem like this before, so I'm not certain what to do. I'm learning Python from Eric Matthes' Python Crash Course, the 2016 edition, if that's any reference. This problem hasn't appeared in the other lessons that I've done from it either.
Here's what I've tried:
I've tried fiddling around with the give_raise(self, salary_raise=None) method, and changing up how that worked in case I had messed something up internally there but I don't see why that would affect the testing.
I've tried deleting it and rewriting it once again (since it's not a lot of code) in hopes that I just forgot something silly, but if I did then I forgot it a second time.
Apologies in advance if it's a very simple fix, and apologies if there's anything wrong with the way I've formatted this question, or if this isn't a forum for questions like this--this is my first time posting here.
-
I apologize that I didn't add docstrings to my functions for clarification on what they do.LGrillos– LGrillos2017年11月10日 22:25:57 +00:00Commented Nov 10, 2017 at 22:25
-
prefix your test methods with 'test' to get them to run.The Stupid Engineer– The Stupid Engineer2017年11月10日 22:31:01 +00:00Commented Nov 10, 2017 at 22:31
3 Answers 3
Test methods need to have names beginning with test_ for the unittest runner to find them.
Call your tests test_give_default_raise and test_give_custom_raise.
1 Comment
The class EmployeeTest(unittest.TestCase):, should have test_ methods to be run the unittest module. so try this:
import unittest
from Chap11Lesson2 import Employee
class EmployeeTest(unittest.TestCase):
def setUp(self):
"""Setting up a variable for use in the test methods"""
self.employee1 = Employee("Lucas", "Grillos", 20000)
def test_give_default_raise(self):
"""Testing to see if 5000 is added properly"""
money = 25000
self.employee1.give_raise()
self.assertEqual(self.employee1.salary, money)
def test_give_custom_raise(self):
"""Testing to see if 10000 is raised properly"""
money = 35000
self.employee1.give_raise(10000)
self.assertEqual(self.employee1.salary, money)
unittest.main()
Comments
Change your method names so they're prefixed with 'test' for the test methods and it should run.