092-Python Unit Test (2)
Today we're going to learn Lesson 2 of Python Unit Testing
Let's start with an example
class Employee: raise_amt = 1.05 def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay @property def email(self): return '{}.{}@email.com'.format(self.first, self.last) @property def fullname(self): return '{} {}'.format(self.first, self.last) def apply_raise(self): self.pay = int(self.pay * self.raise_amt)
How to deal with the three methods in this example
1.email
2.fullname
3.apply_raise
For testing,
Simply, use the assert_you've learnedEqual is OK
Let's write out the test code
Note that the naming must be test_xxx.py
For example, if it isEmployee.pyThen the test file is test_employee.py
import unittest from emp import Employee class TestEmp(unittest.TestCase): def test_email(self): emp_1 = Employee('Bill', 'Gates', 1000) emp_2 = Employee('Steve', 'Jobs', 2000) self.assertEqual(emp_1.email, 'Bill.Gates@email.com') self.assertEqual(emp_2.email, 'Steve.Jobs@email.com') def test_fullname(self): emp_1 = Employee('Bill', 'Gates', 1000) emp_2 = Employee('Steve', 'Jobs', 2000) self.assertEqual(emp_1.fullname, 'Bill Gates') self.assertEqual(emp_2.fullname, 'Steve Jobs') def test_apply_raise(self): emp_1 = Employee('Bill', 'Gates', 1000) emp_2 = Employee('Steve', 'Jobs', 2000) emp_1.apply_raise() emp_2.apply_raise() self.assertEqual(emp_1.pay, 1050) self.assertEqual(emp_2.pay, 2100)
They are all tested using the assertEqual method
In this test code, we found that
In each test method, we wrote
emp_1 = Employee('Bill', 'Gates', 1000) emp_2 = Employee('Steve', 'Jobs', 2000)
This is obviously redundant,
How do you reuse these two lines of code to create objects in each test method?
We need to use the setUp method
import unittest from emp import Employee class TestEmp(unittest.TestCase): def setUp(self): print('setUp') self.emp_1 = Employee('Bill', 'Gates', 1000) self.emp_2 = Employee('Steve', 'Jobs', 2000) def test_email(self): print('test_email') self.assertEqual(self.emp_1.email, 'Bill.Gates@email.com') self.assertEqual(self.emp_2.email, 'Steve.Jobs@email.com') def test_fullname(self): print('test_fullname') self.assertEqual(self.emp_1.fullname, 'Bill Gates') self.assertEqual(self.emp_2.fullname, 'Steve Jobs') def test_apply_raise(self): print('test_apply_raise') self.emp_1.apply_raise() self.emp_2.apply_raise() self.assertEqual(self.emp_1.pay, 1050) self.assertEqual(self.emp_2.pay, 2100)
As in the code above, we added the setUp method.
The setUp method is executed before each test method is executed.
So before each test method is executed, self is given two Employee s
Let's run the test code
setUp test_apply_raise setUp test_email setUp test_fullname Ran 3 tests in 0.004s OK
We see that,
setUp first, then test method
The setUp method is executed before each test method.
In addition to setUp, there are tearDown methods
The tearDown method is executed after each test method.
In addition, there are setUpClass and tearDownClass methods, and the difference between them is
The class method is before and after the entire class
Let's look at the code
import unittest from emp import Employee class TestEmp(unittest.TestCase): @classmethod def setUpClass(cls): print('setUpClass') @classmethod def tearDownClass(cls): print('tearDownClass') def setUp(self): print('setUp') self.emp_1 = Employee('Bill', 'Gates', 1000) self.emp_2 = Employee('Steve', 'Jobs', 2000) def tearDown(self): print('tearDown') def test_email(self): print('test_email') self.assertEqual(self.emp_1.email, 'Bill.Gates@email.com') self.assertEqual(self.emp_2.email, 'Steve.Jobs@email.com') def test_fullname(self): print('test_fullname') self.assertEqual(self.emp_1.fullname, 'Bill Gates') self.assertEqual(self.emp_2.fullname, 'Steve Jobs') def test_apply_raise(self): print('test_apply_raise') self.emp_1.apply_raise() self.emp_2.apply_raise() self.assertEqual(self.emp_1.pay, 1050) self.assertEqual(self.emp_2.pay, 2100)
Look at the results
setUpClass setUp test_apply_raise tearDown setUp test_email tearDown setUp test_fullname tearDown tearDownClass Ran 3 tests in 0.005s OK
Thus, the class method begins and ends with
setUp and tearDown before and after