Python unittest Framework Comprehension and summary (II.)

Source: Internet
Author: User
Tags assert

UnitTest Fundamentals:

? The entire platform is built using Python's unittest test framework, which briefly introduces the simple application of the UnitTest module.

UnitTest is the standard test library for Python, which is the most widely used test framework in Python compared to other test frameworks.

UnitTest has four more important concepts that are test fixture, test case, test suite, Test Runner,.

? Test Fixture:the Test fixture is everything we need to the exercise. The simple thing is to do something that needs to be prepared during the test, such as creating a temporary database, files, and directories, where SETUP () and Setdown () are the most common methods

? Test Case: The base class for the user-defined test cases, called the Run () method, which in turn calls the Setup method, the method of execution, and the TearDown () method.

? Test suite: A collection of test cases that can be manually increased by the Addtest () method, or automatically added by Testloader test Case,testloader when adding a use case, there is no order.

? Test Runner: The driver class that runs the test case, you can perform testcase, or you can execute testsuite. TestCase and Testsuite automatically manage testresult after execution.

Other properties of the Unitest:

? Unittest.skip (): Adorner, when you run a use case, some use cases may not want to execute, etc., and a decorator can be used to temporarily mask the test case. One common usage is that you want to debug a test case, and you want to block other use cases before you can mask them with adorners.

      @unittest.skip (reason): skip (reason) Adorner: 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.

The properties of the 2.TestCase class are as follows:

The setup (): Setup () method is used for the initialization work before the test case is executed. If you need access to a database in a test case, you can establish a database connection and initialize it in Setup. If the test case requires a login to the Web, you can instantiate the browser first.

? TearDown (): The TearDown () method is used for the aftermath of the test case execution. such as closing the database connection. Close the browser.

? assert* (): Some assertion methods: in the process of executing a test case, whether the final use case is carried out is determined by judging the actual result of the test and whether the expected result is equal.

Assertequal (Information printed on a,b,[msg= ' Test failure '): asserts whether 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.

Through Dir (unittest), we can see unittest all the properties and methods of their relationship as shown.

  UnitTest Main class relationships:

The process of normal call to UnitTest is that Testloader automatically loads the test case testcase into Testsuite, Texttestrunner calls Testsuite's Run method, The sequence executes the method that begins with test in the inside TestCase, and gets the result testresult. During the execution of the testcase process, the setup () environment is prepared, the test code is executed, and the final teardown () is tested for restoration.

? where Testloader during the loading process, the testcase to be added is not sequential. A testcase If there are multiple authentication methods, it will be executed according to the first letter of the method.

You can dynamically add testcase by manually calling Testsuite's Addtest, Addtests method, which determines the order in which you add a use case, and avoids the validation method in TestCase must begin with test.

When building an interface test platform, each new interface is automatically generated with a TestCase, and when a single interface is run, a single TestCase is called directly using Texttestrunner. When running multiple interfaces, manually add TestCase to Testsuite for Texttestrunner to call and generate test results.

The overall set of processes is as follows:

The properties of the 3.TestSuite class are as follows: (required for organization of Use cases):

The Addtest (): Addtest () method is to add test cases to the test suite, as below, to add test_baidu test cases under the Baidutest class under the Test_baidu module to the test suite.

Suite = UnitTest. TestSuite () suite.addtest (Test_baidu. Baidutest ('test_baidu'))

The properties of 4.TextTextRunner are as follows: (required for organization of Use cases):

 The Run (): Run () method is the test case that runs the test suite and is included in the Suite test suite.

Runner = UnitTest. Texttestrunner () Runner.run (suite)

The 5.unitest directory structure is generally as follows: Important

The whole of this unitest is finished.

Python unittest Framework Comprehension and summary (II.)

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.