Python has a self-contained unit test framework that is the UnitTest module, which is used for unit testing, which encapsulates some of the result methods returned by the checksum and some initialization operations before the use cases are executed.
Before you say UnitTest, say a few concepts:
TestCase is also a test case
TestSuite Multiple test cases are assembled together, that is TestSuite
Testloader is used to load testcase into Testsuite.
Texttestrunner is to execute a test case, the results of the test are saved to the Texttestresult instance, including how many test cases were run, how much success, how many failed, etc.
A simple unit test case is written below:
Here are some common assertions, that is, verifying the results
How to generate a test report, you need to add another module, Htmltestrunner, this module needs to install itself, using the execution of test cases will generate an HTML test report, there will be the results of each test case execution, the code is as follows:
If we have a lot of modules, each module is written under a lot of Python files, each python file has a test case, how to put the use case in this directory is executed, we must first find all the Python files in this directory, and then find the inside of the test cases, executed, The code is as follows:
Python Unit Test UnitTest