Overview
1. Testing Scaffold (test fixture)
The work to be done before the test is prepared and the work to be done after the test is completed. Includes setup () and teardown (). 2. Testing cases (test case)
The smallest test unit. 3. Testing Suites (test suite)
A collection of test cases. 4. Test Runner
The component that the test executes. command-line interface
You can run test modules, test classes, and test methods using the command line.
Python-m unittest test_module1 test_module2 python-m unittest
. TestClass
python-m unittest test_module. Testclass.test_method
Can add-V print details
Python-m unittest-v test_module test Case automatic Search
UnitTest supports a simple test discovery. After the command line is passed in discovery, the framework automatically searches the current directory for the case to test and executes it. The search directory must be a package or module. The basic use is as follows:
CD project_directory
python-m unittest Discover
Sub options are as follows:
-v,–verbose
Detail level of output information
-s,–start-directory Directory
Start the search directory (default is the current directory)
-p,–pattern pattern
Matching file name (default is test*.py)
-t,–top-level-directory Directory
Search for the top-level directory (default is start directory) to Create test code 1. Way One
Create subclass Inheritance UnitTest. TestCase, and then rewrite the following methods
Class Widgettestcase (UnitTest. TestCase):
def setUp (self): Pass def runtest (self): pass
def teardown (self):
Pass
Run 2. Mode two
Write a method that starts with test
Class Widgettestcase (UnitTest. TestCase):
def setUp (self):
pass
def test_xx1 (self)
def test_xx2 (self)
... def test_xxn (self)
def teardown (self):
Pass
Building test Suites
Way One
Widgettestsuite = UnitTest. TestSuite ()
widgettestsuite.addtest (widgettestcase (' test_default_size '))
widgettestsuite.addtest ( Widgettestcase (' test_resize '))
mode two (recommended)
Def suite ():
suite = UnitTest. TestSuite ()
suite.addtest (widgettestcase (' test_default_size '))
suite.addtest (widgettestcase (' Test_ Resize ') return
Suite
Mode III (recommended)
Def suite ():
tests = [' test_default_size ', ' test_resize '] return
unittest. TestSuite (Map (widgettestcase, tests))
Mode four
Multiple test suites built into larger test suites
Suite1 = Module1. Thetestsuite ()
suite2 = Module2. Thetestsuite ()
alltests = UnitTest. TestSuite ([Suite1, Suite2])
Mode five
UnitTest's Testloader provides a default test suite
Suite = UnitTest. Testloader (). Loadtestsfromtestcase (Widgettestcase)
Ignore test Cases (Python2.7 support)
Can be divided unconditionally ignore and conditional ignore, through the adorner implementation
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 to 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 ignore
@unittest. Skip ("Showing class skipping")
class Myskippedtestcase (unittest. TestCase):
def test_not_run (self):
Pass