Unit testing framework (2)

Basic characteristics of unit testing

important concepts

 

1.Test Case

  A class is a C++ tool for converting abstractions into user-defined types, combining data representation and methods of manipulating data into a neat package. An object is an instance of a class. An instance of TestCase is a test case. What is a test case? A test case is a complete test process, including the setup of the pre-test preparation environment (setUp), the code to implement the test process (run), and the restoration of the test environment (tearDown). The essence of unit test: a test case is a complete test unit. By running the test unit, a function is verified to meet the expected results.

2.Test Suite

  Verification of a function often requires multiple test cases, and the test suite TestSuite can integrate multiple test cases for execution. Test cases can be added through the addTest() method provided by the TestSuite class.

3.Test Runner

  Execution of tests—General unit testing frameworks provide rich execution strategies and execution results. In the unittest unit testing framework, the test suite / test case is executed through the run() method provided by the TextTestRunner class . The test runner can use a graphical interface, a text interface, or return a special value to represent the result of the test execution.

4.Test Fixture

  A fixture means the construction and destruction of a test case environment by overriding the setUp() and tearDown() methods of the TestCase class respectively. what's the function? In many cases, when we execute a test case, we will first check the database to see if the user meets the initial conditions for the execution of this test case. To give a simple example, I want to test the withdrawal function, and the withdrawal function is only open to certified members, then I will access the database at this time. If the user is an ordinary member, then I will first modify the corresponding key value to meet the requirements. Initial conditions for functional testing. For example, in this test case, we need to access the database, then we can establish a database connection in setUp() for initialization, and clear the data generated by the database in tearDown(). The earDown() process is important to leave a clean environment for the next test case.

 

 

>>>Optimize the unit test case of the unit test framework (1)<<<

First, attach the tested class Count in the unit testing framework (1)

1  # calculator.py 
2  
3  #Calculator class 
4  class Count:
 5  
6      #Initialize two numbers by __init()__ method 
7      def  __init__ (self,a,b):
 8          self.a = float( a)
 9          self.b = float(b)
 10  
11      #addition 12 def add 
(self):
 13 return round(self.a + self.b,2)              

 

 

After combining the above concepts, the improved unit test case is as follows:

1  # unittest_calculator.py 
2  
3  from calculator import Count
 4  import unittest
 5  # python2.1 and later versions have put unittest as a standard module in the python development package 
6  
7  #Create a TestCount class to inherit the TestCase class of the unittest module 
8  class TestCount(unittest.TestCase):
 9  
10      #Build the test case environment 
11      def setUp(self):
 12          print ( " test start " )
 13  
14      #Test the addition of two floating -point numbers 
15      def test_add1(self):
16          
17          # tep1. Call the Count class and pass in the number to be calculated (3.111,6.229) 
18          c = Count(3.111,6.229 )
 19          # tep2. Call the add() method in the Count class to get the return value of the addition of the two numbers 
20          # tep3. Call the assertEqual() method provided by the unittest framework to assert the return value of add() to determine whether the program execution result is consistent with the expected value, eliminating the previous cumbersome exception handling 
21          self.assertEqual(c.add( ), 9.34 )
 22  
23      def test_add2(self):
 24          c = Count(3,6 )
 25          self.assertEqual(c.add(),9.00 )
 26  
27      def Test_add3(self):
 28          c = Count(3.5,6.5 )
 29         self.assertEqual(c.add(),10.00 )
 30  
31      #Destruction of test case environment 
32      def tearDown(self):
 33          print ( " test end " )
 34  
35  
36  # __name__ as the calling method of the module, if equal to _ _main__, it means to directly call 
37  if  __name__ == ' __main__ ' :
 38  
39      #Build test set 
40      suite = unittest.TestSuite()
 41      suite.addTest(TestCount( " test_add2 " ))
 42     suite.addTest(TestCount( " Test_add3 " ))
 43  
44      #Call the run() method of the TextTestRunner() class of the unittest framework to run the test cases assembled by the suite 
45      runner = unittest.TextTestRunner()
 46      runner.run(suite)

 

 

operation result:

======= RESTART: C:\Haauleon\Unittest\20180422\unittest_calculator.py =======
test start
test end
.test start
test end
.
----------------------------------------------------------------------
Ran 2 tests in 0.075s

OK

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324656176&siteId=291194637