6-1 Unit Testing Overview
Unit testing refers to the inspection and verification of the smallest testable unit of software. for unit testing
In general, the meaning of the middle order should be determined according to the actual situation. For example, a unit in C language refers to a function.
In Java, a unit refers to a class, and in graphical software, it can refer to a window or a menu. In general, the unit is the person
It is the specified minimum tested functional module.
unit testing framework
Before the advent of unit testing frameworks, developers struggled to create executable tests. The initial approach is to apply
A window is created in the program with a "test harness". It is just a window, one for each test
button. The result of these tests is either a message box or some kind of display result directly on the form itself.
Since each test requires a button, these windows can quickly become crowded and unmanageable.
Unit testing frameworks provide a unified programming model to define tests as simple classes with methods in those classes
You can call the application code you wish to test. Developers do not need to write their own test control tools; unit
The testing framework provides a test runner that executes all tests with the click of a button. Utilize unit tests
Framework that makes it easy to plug in, set up, and decompose test-related functionality. When a test fails, the test runner can provide
Information about the failure, including any exploitable exception information and stack trace. Different programming languages have different
Unit testing frameworks, such as Java's Junit, TestNg, C#'s Nunit, Python's Unittest, Pyunit, testtools, subunit...
unit testing framework
Provide use case organization and execution
Provides rich assertion methods
Provide rich logs and test results
Python unit testing framework -- unittest
unittest official documentation https://docs.python.org/2.7/library/unittest.html
The unittest unit testing framework can be applied not only to unit testing, but also to the development of WEB automated test cases
and execution, the test framework can organize and execute test cases, and provides a wealth of assertion methods to determine whether test cases are
Pass, and finally generate the test result.
6-2 Introduction to the core elements of unittest
1.TestCase
An instance of TestCase is a test case. What is a test case? is a complete testing process.
Including preparing the environment before the test (setUP), executing the test code (run), and restoring the environment after the test
(tearDown), the essence of unit test (unit test) is here, a test case is a complete test unit.
By running this test unit, a problem can be verified.
2.TestSuite
A collection of multiple test cases is called TestSuite, and TestSuite can also nest TestSuite.
TestLoader is used to load test cases into TestSuite.
3.TextTestRunner
TextTestRunner is to execute test cases, in which run() will execute the tests in TestSuite/TestCase
run(result) method. The result of the test is saved to the TextTestResult instance, including how many test cases were run
, how many successes, how many failed, etc.
4.Fixture
The construction and destruction of a test case environment is a fixture.
unittest case
Constructs a class Math containing integer addition operations
calculator.py
class Math: def __init__(self,a,b): self.a=int(a) self.b=int(b) def add(self): return self.a + self.b
from calculator import Math import unittest class TestMath(unittest.TestCase): def setUp(self): print("start test") def test_add(self): j=Math(5,10) #self.assertEqual(j.add(),15) #use case failure scenario self.assertEqual(j.add(),12) def tearDown(self): print("test end") if __name__=='__main__': #Construct test set suite=unittest.TestSuite() suite.addTest(TestMath("test_add")) #Execute the test runner=unittest.TextTestRunner() runner.run(suite)
6-4 Common Assertion Methods
The content of the assertion is an important part of the automation script. Only after the assertion is set correctly can it help us judge the execution result of the test case.
Assertion method
assertEqual(a,b) Judging a==b
assertNotEqual(a,b) Judgment a!=b
assertTrue(x) bool(x) is True
assertFalse(x)
bool(x) is False
assertIs(a,b) a is b
assertIsNot(a,b) a is not b
assertIsNone(x)
x is None
assertIsNotNone(x) x is not None
assertIn(a,b)
a in b
assertNotIn(a,b)
a not in b
assertIsInstance(a,b) isinstance(a,b)
assertNotIsstance(a,b)not isinstance(a,b)
from calculator import Math import unittest class TestMath(unittest.TestCase): def setUp(self): print("start test") # def test_add(self): # j=Math(5,10) # self.assertEquals(j.add(),12) # def test_add(self): # j=Math(5,10) # self.assertNotEqual(j.add(),12) # def test_add(self): # j=Math(5,10) # self.assertTrue(j.add()>10) # def test_add(self): # j=Math(5,10) # self.assertIn("888","hello test") def test_add(self): j=Math(5,10) self.assertIs("test","test") self.assertIs("8888","888") def tearDown(self): print("test end") if __name__=='__main__': #Construct test set suite=unittest.TestSuite() suite.addTest(TestMath("test_add")) #Execute the test runner=unittest.TextTestRunner() runner.run(suite)
6-5 6-6 New test case management
The front is for unit testing a single add method. If multiple methods are required for testing, what should I do?
Such as adding a Sub method for unit test verification.
class Math: def __init__(self,a,b): self.a=int(a) self.b=int(b) def add(self): return self.a + self.b def sub(self): return self.a-self.bUnit test verification of add and sub methods at the same time
from calculator import Math import unittest class test_add(unittest.TestCase): def setUp(self): print("test start") def test_add(self): j=Math(5,5) self.assertEquals(j.add(),10) def test_add1(self): j=Math(10,20) self.assertEquals(j.add(),30) def tearDown(self): print("test end") class test_sub(unittest.TestCase): def setUp(self): print("start test") def test_sub(self): i = Math(8, 8) self.assertEquals(i.sub(), 0) def test_sub1(self): i = Math(5,3) self.assertEquals(i.sub(), 2) def tearDown(self): print("test end") if __name__=='__main__': #Construct test set 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")) #Execute the test runner=unittest.TextTestRunner() runner.run(suite)
6-7 Use case common part merge
In the previous lesson, each test class has SetUp() and tearDown() methods, and the contents of both methods are
The same, for printing the start and end prompt statements, can they be combined together?
from calculator import * import unittest class Test_StartEnd(unittest.TestCase): def setUp(self): print("test start") def tearDown(self): print("test end") class TestAdd(Test_StartEnd): def test_add(self): j=Math(5,5) self.assertEquals(j.add(),10) class TestSub(Test_StartEnd): def test_sub(self): i=Math(5,5) self.assertEquals(i.sub(),0) if __name__ == '__main__': unittest.main()
6-8 Use Case Execution Sequence
import unittest class Test1(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("Test1 end!") class Test2(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__': # unittest.main() suite=unittest.TestSuite() suite.addTest(Test1('test_c')) suite.addTest(Test1("test_b")) suite.addTest(Test2("test_d")) suite.addTest(Test2("test_a")) runner=unittest.TextTestRunner() runner.run(suite)
Execution order rules--numeric and alphabetical order of test classes or test methods 0~9, A~Z
Execute test cases in specified order
call.py
from case_order import * import unittest suite = unittest.TestSuite() suite.addTest(Test1("test_c")) suite.addTest(Test1("test_b")) suite.addTest(Test2("test_d")) suite.addTest(Test2("test_a")) runner = unittest.TextTestRunner() runner.run(suite)
6-9 Use Case Integrated Framework Management (1)
The previous test cases and execution are written in a file. When the number of use cases continues to increase each month, the execution and management of use cases
It becomes very cumbersome, so it is necessary to use a separate module to manage the use case according to the specific functional module. like a
The same is true for schools to manage classes according to different grades.
Case: The Test_Projice file directory contains 4 python files: