How to use Python unit testing framework unittest

  • 2020-05-09 18:45:17
  • OfStack

An overview of the

1. Test scaffolding (test fixture)

What to do before test preparation and what to do after test execution, including setUp() and tearDown().

2. Test cases (test case)

The smallest test unit.

3. Test suite (test suite)

A collection of test cases.

4. Test runner (test runner)

Test the executing component.

Command line interface

You can use the command line to run test modules, test classes, and test methods.


python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

Addable -v prints details

python -m unittest -v test_module

Automatic test case search

unittest supports simple test discovery. When the command line is passed into discovery, the framework will automatically search the current directory for the case to be tested and execute it.


cd project_directory
python -m unittest discover

The suboptions are as follows:
- v � verbose
The level of detail of the output information

- s � start - directory directory
Start searching directory (default is current directory)

- p � pattern pattern
Matching file name (default is test*.py)

- t � top level - directory directory
Top level directory for search (default is start directory)

Create test code

1. 1

Create a subclass to inherit unittest.TestCase, and then override the following method


class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        pass
    def runTest(self):
        pass
    def tearDown(self):
        pass

run

2. 2

Write methods that begin 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

Method 1


widgetTestSuite = unittest.TestSuite()
widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
widgetTestSuite.addTest(WidgetTestCase('test_resize'))

Approach 2(recommended)


def suite():
    suite = unittest.TestSuite()
    suite.addTest(WidgetTestCase('test_default_size'))
    suite.addTest(WidgetTestCase('test_resize'))
    return suite

Approach 3(recommended)

def suite():
    tests = ['test_default_size', 'test_resize']
    return unittest.TestSuite(map(WidgetTestCase, tests))

Methods 4

Multiple test suites are built into larger test suites


suite1 = module1.TheTestSuite()
suite2 = module2.TheTestSuite()
alltests = unittest.TestSuite([suite1, suite2])

Method 5

unittest's TestLoader provides the ability to generate default test suites


suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)

Ignore test cases (Python2.7 support)

Can divide unconditionally ignore and conditional ignore, through decorator realization


python -m unittest -v test_module
0
Test classes can also be ignored

@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
    def test_not_run(self):
        pass


Related articles: