Python Practical unittest Use

Source: Internet
Author: User

What is a unittest?
UnitTest is a built-in unit testing framework for Python, with conditions for writing automated frameworks such as use cases, organizational use cases, execution cases, and output reports. Before using unittest, you need to understand the five concepts of the framework: Test Case,test suite,testloader,test runner,test fixture.
Test Case: A complete testing unit that executes the test unit to complete the validation of a problem: pre-test environment preparation (setUp), Execution of test code (run), and post-test environment restore (tearDown); test suite : A collection of multiple test cases, a test suite, or a test plan; Testloader: Loads testcase into Testsuite, where the loadtestsfrom__ () method is used to find testcase and create instances of them, and then add them to the T Estsuite, the Testsuite instance is returned; Test Runner: Executes the test case and saves the test results to the Texttestresult instance, including the number of test cases that were run, the number of successes, the number of failures, and so on; Test fixt Ure: Initial preparation and environmental restoration of a test case, mainly the setup () and Setdown () methods;


How the two unitest work
By invoking analysis with the UnitTest class, you can outline the workflow of the framework as follows:
Write TestCase, loaded by Testloader testcase to Testsuite, then run Texttestrunner by Testsuite, and finally save the running results in Texttestresult.
Three UnitTest practical examples
After understanding the five concepts and workflows of the unittest framework, let's follow an example of how to use the framework to Unit-test modules.
3.1 Module to be tested MyFunc
MyFunc contains three functions: IsPrime (), add (), divide (), as follows:


3.2 Unitest Writing test Cases
Using UnitTest to Unit test MyFunc, you first need to import the Unitest framework and the module to be tested MyFunc, the defined test case method class needs to inherit UnitTest. TestCase, and the test case method is identified with the start of the tests, the execution result of the use case is determined by the assetxxx assertion result, and if the assertion returns false, the Asseterror exception is thrown. The test case code is as follows:

Some common assertion methods are as follows, and for more assertion methods, see the UnitTest Documentation:

From the test case execution results of the above design and the requirements for automated testing, the following 4 questions need to be considered, and the workarounds in the Unitest framework are given.
3.3 How the framework solves 4 problems with automation requirements
3.3.1 Question 1: How to control the order of use case execution
In UnitTest, the use case is defined by the method that begins with test, and the default execution order is based on the use case name ascending, as in the case above, the actual order of execution is: Test_add-->test_divide-->test_is_prime, Rather than the order in which the use case is defined. The problem with resolving the order of use cases in UnitTest is to use Testsuite, which is the following code:

3.3.2 Question 2: How to use multiple use cases to share setup, teardown
UnitTest Setup, teardown will be executed once before and after each use case, as there are 3 test cases in the test case class above, then the setup will be executed before each use case executes, and execution will execute teardown, that is, Setup, teardown will call three times altogether. , but consider the actual automated test scenario, multiple use cases only need to perform one setup once, all the use cases after the completion of the execution of a teardown, for this scenario, unittest processing method is to use SetupClass, Teardownclass, Note the use of @classmethod, as follows:

3.3.3 Question 3: How to skip a use case
In automated testing, often encountered in the selection of use cases, the solution in UnitTest is to use the skip adorner, where the skip adorner has 3 main types: unittest.skip (reason), unittest.skipif (condition, Reason), unittest.skipunless (Condition,reason), which skips the use case under condition conditions, reason is used to describe the reason for the skip, the instance code is as follows:

3.3.4 Question 4: How to generate a test report in HTML format
The report format generated by default in UnitTest is TXT, if you want to generate HTML-formatted reports, you can use the Htmltestrunner module, import the module after installation, use Htmltestrunner instead of the default Texttestrunner () Execute the test case. The instance code is as follows:


Other resources:

For a video explanation of the Python unittest cell framework, see http://i.youku.com/weiworld521 23rd.


Python Practical unittest Use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.