First, add test cases
calculator.py class Math: def __init__ (self,a,b): int(a) int(b) def add ( Self): return self.a + self.b def sub (self): return SELF.A- self.b
Test
test_math.py fromCaculator Import *Import UnitTestclassTest_add (unittest. TestCase): def setUp (self): print ("Test is start") def test_add (self): J=math (5,5) self.assertequal (J.add (),Ten) def test_add1 (self): J=math (Ten, -) self.assertequal (J.add (), -) def tearDown (self): print ("test is end!")classtest_sub (unittest. TestCase): def setUp (self): print ("Test is start") def test_sub (self): I=math (8,8) self.assertequal (I.sub (),0) def test_sub1 (self): I=math (5,3) self.assertequal (I.sub (),2) def tearDown (self): print ("test is end!")if__name__ = ='__main__': Suite=UnitTest. TestSuite () suite.addtest (Test_add ("Test_add")) Suite.addtest (Test_add ("TEST_ADD1")) Suite.addtest (Test_sub ("test_sub") ) #suite. Addtest (Test_sub ("Test_sub1")) Runner=UnitTest. Texttestrunner () Runner.run (suite)
Ii. test the order of pressure execution
Self-Bringing order:
Import UnitTestclassTest2 (unittest. TestCase): def setUp (self): print ("Test1 Start") def test_c (self): print ("Test_c") def test_b (self): print ("Test_b") def tearDown (self): print ("Test End")classTest1 (unittest. TestCase): def setUp (self): print ("Test2 Start") def test_d (self): print ("Test_d") def test_a (self): print ("test_a") def tearDown (self): print ("Test2 end!")if__name__=='__main__': if__name__ = ='__main__' : Unittest.main ()
// Execution Order rule-number and alphabetical order of test class or test method 0~9,a-z
Custom order: Using Test sets
Three, use case comprehensive framework management
The previous test cases and executions are written in a single file, and when the number of use cases increases, the execution and management of the use cases becomes cumbersome and requires a separate module to be managed for use cases based on specific functional modules. Just as a school has to be divided into classes according to different grades.
Case: The Test_project file directory contains 4 python files:
- l--startend.py-- SetUp and TearDown Management
- Implementation of l--calculatory.py--addition and subtraction operation method
- l--test_add.py--addition Test Cases
- l--test_sub.py--Subtraction test Case
- l--runtest.py--use case Execution management
PYTHON-ZX Note 11-Test pressure management