Python UnitTest Module Experience

Source: Internet
Author: User

Some experience about UnitTest module, mainly is the example of crossing net, add a little own understanding, official website address:https://docs.python.org/3.6/library/unittest.html

The UnitTest module is a Python unit test framework that supports automated testing, and all use cases share setup and teardown code, and test cases can be aggregated into test sets, and test cases are independent of the reporting framework.

To implement these features, UnitTest supports the following object-oriented concepts:

    1. Test Fixture: Represents the preparation required to perform one or more test cases, and all related cleanup efforts. For example, create a temporary or proxy database, a folder, or start a server.
    2. Test Case: Represents the individual unit test cases, and the feedback corresponding to the input parameters is checked. UnitTest provides a base class testcase for creating test cases.
    3. Test suite: Represents a collection of testing cases and test suites that are used to aggregate test cases together for execution.
    4. Test Runner: Used to execute the testing case and feedback the test results to the user. You can use a graphical interface, a text interface, or return some specified values to indicate test results.
    5. Test report: You can use UnitTest's own Texttestrunner (), or you can use Htmltestrunner () to produce an HTML-formatted beta, now with Bstestrunner () Instead of Htmltestrunner ()

The relationships between these concepts are as follows:

Here are some examples and explanations:

ImportUnitTestclassTeststringmethods (unittest. TestCase): # # #编写的测试类要继承unittest.    TestCase, the class name begins with test and is explained later. defsetUp (self): # # #所有用例共用的setup, executed before the use case is executed to build the environment. Print('Case Setup')    defTearDown (self): # # #所有用例共用的tearDown, executed after the execution of the use case, to clean up the environment.        Setup and teardown ensure that each test case is independent and not dependent on the other case. Print('Case teardown\n')    defTest_upper (self): # # #测试用例1, you need to start with test, for the following reason. Print('Case Test_upper') self.assertequal ('Foo'. Upper (),'FOO'# # #assertEqual (A, B) if a==b is the use case pass, otherwise fail. defTest_isupper (self): # # #测试用例2, you need to start with test. Print('Case Test_isupper') Self.asserttrue ('FOO'. Isupper ()) # # #类似的assert用法 Self.assertfalse ('Foo'. Isupper ())defTest_split (self): # # #测试用例3Print('Case Test_split') s='Hello World'self.assertequal (S.split (), ['Hello',' World'])        #Check that s.split fails when the separator are not a stringWith self.assertraises (TypeError): S.split (2)if __name__=='__main__': Unittest.main () # # #具体做了哪些工作后面会详细讲. 

Let's look at the results of the case:

Case Setupcase test_isuppercase teardowncase setupcase test_splitcase teardowncase setupcase test_uppercase teardown

# # #三个case全部pass, because the results in the assert are correct. The test results are not shown here, but the printed information is displayed. We can see that the case for each test_ is running on one side,
Each time the setup and Teardown.case are called independently, the order of execution is not in the order of the Code, but in the order of the case name letters, which is determined by the Unittest.main () function.

Python UnitTest Module Experience

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.