This article describes how to use unittest in the Python unit test framework. This article describes unittest overview, command line interface, test case automatic search, test code creation, and test suite creation methods, for more information, see
Overview
1. test fixture)
Work to be done before test preparation and after test execution, including setUp () and tearDown ().
2. test case)
Minimum test unit.
3. test suite)
A set of test cases.
4. test runner)
Testing component.
Command line interface
You can run the test module, test classes, and test methods using the command line.
The code is as follows:
Python-m unittest test_module1 test_module2
Python-m unittest test_module.TestClass
Python-m unittest test_module.TestClass.test_method
Add-v to print details
The code is as follows:
Python-m unittest-v test_module
Test case search
Unittest supports simple test discovery. after the command line is passed into the discovery, the framework will automatically search for the case to be tested in the current directory and execute. The search directory must be a package or module. The basic usage is as follows:
The code is as follows:
Cd project_directory
Python-m unittest discover
Sub-options are as follows:
-V,-verbose
Details of output information
-S,-start-directory
Start searching for the Directory (the current directory by default)
-P,-pattern
Matched file name (test *. py by default)
-T,-top-level-directory
Top-level directory to be searched (start directory by default)
Create test code
1. Method 1
Create a subclass to inherit unittest. TestCase, and then override the following method:
The code is as follows:
Class WidgetTestCase (unittest. TestCase ):
Def setUp (self ):
Pass
Def runTest (self ):
Pass
Def tearDown (self ):
Pass
Run
2. Method 2
Compile a method starting with test
The code is as follows:
Class WidgetTestCase (unittest. TestCase ):
Def setUp (self ):
Pass
Def test_xx1 (self)
Def test_xx2 (self)
...
Def test_xxN (self)
Def tearDown (self ):
Pass
Build test suite
Method 1
The code is as follows:
WidgetTestSuite = unittest. TestSuite ()
WidgetTestSuite. addTest (WidgetTestCase ('test _ default_size '))
WidgetTestSuite. addTest (WidgetTestCase ('test _ resize '))
Method 2 (recommended)
The code is as follows:
Def suite ():
Suite = unittest. TestSuite ()
Suite. addTest (WidgetTestCase ('test _ default_size '))
Suite. addTest (WidgetTestCase ('test _ resize '))
Return suite
Method 3 (recommended)
The code is as follows:
Def suite ():
Tests = ['test _ default_size ', 'Test _ resize']
Return unittest. TestSuite (map (WidgetTestCase, tests ))
Method 4
Build multiple test suites into larger test suites
The code is as follows:
Suite1 = module1.TheTestSuite ()
Suite2 = module2.TheTestSuite ()
Alltests = unittest. TestSuite ([suite1, suite2])
Method 5
The TestLoader of unittest provides a default test suite.
The code is as follows:
Suite = unittest. TestLoader (). loadTestsFromTestCase (WidgetTestCase)
Ignore test cases (supported by Python2.7)
It can be divided into unconditional and conditional ignoring, implemented through the decorator
The code is as follows:
Class MyTestCase (unittest. TestCase ):
@ Unittest. skip ("demonstrating skipping ")
Def test_nothing (self ):
Self. fail ("shouldn't happen ")
@ Unittest. skipIf (mylib. _ version _ <(1, 3 ),
"Not supported in this library version ")
Def test_format (self ):
# Tests that work for only a certain version of the library.
Pass
@ Unittest. skipUnless (sys. platform. startswith ("win"), "requires Windows ")
Def test_windows_support (self ):
# Windows specific testing code
Pass
The test class can also be ignored.
The code is as follows:
@ Unittest. skip ("showing class skipping ")
Class MySkippedTestCase (unittest. TestCase ):
Def test_not_run (self ):
Pass