Python-Brief UnitTest framework

Source: Internet
Author: User

Brief introduction of UnitTest framework

The UnitTest unit test framework can be applied not only to unit testing, but also to the development and execution of automated test cases for Web UI and interface APIs that organize the execution of test cases, and provide a rich assertion method to determine whether test cases pass and ultimately generate test results.

I. Overview of the UNITTEST structure

The four core concepts in UnitTest are: test fixture, test case, test suite, Test Runner/report

1. Test Fixture

①setup (): Run before each test function runs

②teardown (): Executes after each test function finishes running

③setupclass (): Must use @classmethod adorner, all test runs before run

④teardownclass (): Must use @classmethod adorner, run once after all test runs

2, Testcase

A, writing your own test cases must inherit the TestCase class

Import Unittestclass TestCase (unittest. TestCase)

B. Write test case:

def test_001_case1 (self):        print ("Case1") def test_002_case1 (self):        print ("Case2")

Note:

1. The name of the test case must begin with test and the test case not named with test will not be loaded (except when called in test case named by testing)

2. The order of execution of test cases can be customized, if the test cases are named numerically (for example: Test_001_case1, test_002_case1), they are executed numerically, and if they are not numerically named, they are executed in alphabetical order of the English words after test

C, selective execution of test case

"""
@unittest. Skip (reason): skip (reason) Decorator: Skip the decoration test unconditionally and explain why you skipped the test. @unittest. SkipIf (reason): SkipIf (Condition,reason) Adorner: When the condition is true, skip the decorated test and explain the reason for skipping the test. @unittest. skipunless (reason): Skipunless (Condition,reason) Adorner: When the condition is false, skip the decoration test and explain the reason for skipping the test. @unittest. Expectedfailure (): expectedfailure () test marked as failed.
"""

D. Assertions commonly used in test case

"" "Assertequal (Information printed when a,b,[msg= ' Test Failed"): asserts that A and b are equal, and the test case passes. Assertnotequal (Information printed on a,b,[msg= ' Test failure '): asserts that A and B are equal and not equal, the test case passes. Asserttrue (Information printed on x,[msg= ' Test failure '): Asserts if X is true and true if the test case passes. Assertfalse (Information printed on x,[msg= ' Test failure '): asserts if X is false and False if the test case passes. Assertis (Information printed when a,b,[msg= ' Test failed '): Assert whether A is B, is the test case passed. Assertnotis (Information printed when a,b,[msg= ' Test failed '): Assert whether A is B, not the test case passed. Assertisnone (Information printed when x,[msg= ' test fails '): Assert x is None and none is the test case passed. Assertisnotnone (Information printed on x,[msg= ' Test failure '): asserts if X is None, not none, and the test case passes. Assertin (Information printed on a,b,[msg= ' Test failure '): Assert whether A is in B, and in B the test case passes. Assertnotin (Information printed on a,b,[msg= ' Test failure '): Assert whether A is in B, not in B, the test case passes. Assertisinstance (Information printed when a,b,[msg= ' Test failed '): Assertion A is an instance of B, and the test case passes. Assertnotisinstance (Information printed on a,b,[msg= ' Test failure '): Assertion A is an instance of B, not the test case passed. """

E, test case parameterization operation

The test case is not supported by default, so it is necessary to use a third-party module for parametric operation if you want to achieve the effect of the pass parameter.

1. Download

PIP3 Install nose_parameterized

2. Example:

Import unittestfrom nose_parameterized import Parameterizedclass Test (unittest. TestCase):    @parameterized. Expand (        [            [' Xiaogang ', ' 123456 ', True], #可以是list, can also be ganso            [' ', ' 123456 ', True],            [' Xiaogang ', ', ', false],            [' ADGADG ', ' 123456 ', False]        ]    )    def test_login (self,args1,args2 , ARGS3): #这里的参数对应上述列表里的元素, runs through a two-dimensional list of the above lists until all elements are called Run finish        print (ARGS1,ARGS2)
3. Text Suite

Python-Brief UnitTest framework

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.