Python unittest use case execution mode

Source: Internet
Author: User
Tags assert

The simplest way to not explain
Import UnitTest


One, UnitTest module of the various properties of the description

Click to return to table of contents

Let's talk a little bit. UnitTest module of the various properties, so-called to be able to victorious, understand the various properties of unittest, for subsequent writing use cases have a great help.

The properties of 1.unittest are as follows:

[' Basetestsuite ', ' functiontestcase ', ' skiptest ', ' TestCase ', ' testloader ', ' testprogram ', ' testresult ', ' TestSuite ', ' Texttestresult ', ' texttestrunner ', ' _texttestresult ', ' __all__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' __path__ ', ' __unittest ', ' case ', ' defaulttestloader ', ' expectedfailure ', ' findtestcases ', ' Gettestcasenames ', ' installhandler ', ' loader ', ' main ', ' makesuite ', ' registerresult ', ' RemoveHandler ', ' Removeresult ' , ' result ', ' runner ', ' signals ', ' skip ', ' skipIf ', ' skipunless ', ' suite ', ' Util '

Description

UnitTest. TestCase: The TestCase class, which inherits the basic classes of all test case classes.

Class Baidutest (UnitTest. TestCase):

Unittest.main (): Using her to make it easy to change a unit test module into a test script that can be run directly, the main () method uses the Testloader class to search for all the test methods contained in the module that begin with the name "test". and automatically execute them. The default order of execution methods is to load test cases according to the Order of ASCII code, in the order of numbers and letters: 0-9,a-z,a-z. So the test case method that starts with a will take precedence and will be executed after the start of a.

UnitTest. TestSuite (): The TestSuite () class of the UnitTest framework is used to create a test suite.

UnitTest. Texttextrunner (): The Texttextrunner () class of the UnitTest framework, which runs the test cases assembled by the suite through the Run () method under the class, into the Suite test suite.

Unittest.defaulttestloader (): Defaulttestloader () class, the Discover () method below the class can automatically test the catalog Start_ Dir matches the Find test case file (test*.py) and assembles the found test case into a test suite so that the discover can be executed directly from the run () method. Use the following:

Discover=unittest.defaulttestloader.discover (Test_dir, pattern= ' test_*.py ')

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) 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.

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

[' __call__ ', ' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ' , ' __init__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ' , ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' _addskip ', ' _baseassertequal ', ' _classsetupfailed ', ' _deprecate ', ' _ Diffthreshold ', ' _formatmessage ', ' _getassertequalityfunc ', ' _truncatemessage ', ' addcleanup ', ' AddTypeEqualityFunc ' , ' assertalmostequal ', ' assertalmostequals ', ' assertdictcontainssubset ', ' assertdictequal ', ' assertequal ', ' Assertequals ', ' assertfalse ', ' assertgreater ', ' assertgreaterequal ', ' assertin ', ' assertis ', ' assertisinstance ', ' Assertisnone ', ' assertisnot ', ' assertisnotnone ', ' assertitemsequal ', ' assertless ', ' assertlessequal ', ' Assertlistequal ', ' assertmultilineequal ', ' assertnotalmostequal ', ' assertnotalmostequals ', ' assertNotEqual ', ' Assertnotequals ', ' assertnotin ', ' assertnotisinstance ', ' assertnotregexpmatches ', ' assertraises ', ' assertraisesregexp ', ' assertregexpmatches ', ' assertsequenceequal ', ' assertsetequal ', ' asserttrue ', ' Asserttupleequal ', ' assert_ ', ' counttestcases ', ' Debug ', ' Defaulttestresult ', ' docleanups ', ' fail ', ' failif ', ' Failifalmostequal ', ' failifequal ', ' failunless ', ' failunlessalmostequal ', ' failunlessequal ', ' failunlessraises ', ' Failureexception ', ' id ', ' longmessage ', ' MaxDiff ', ' Run ', ' setUp ', ' setupclass ', ' shortdescription ', ' skiptest ', ' TearDown ', ' Teardownclass ']

Description

setup (): The 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.

The properties of the 3.TestSuite class are as follows: (needed when organizing use cases)

[' __call__ ', ' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ' , ' __init__ ', ' __iter__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' _addclassormodulelevelexception ', ' _get_previous_module ', ' _handleclasssetup ', ' _handlemodulefixture ', ' _handlemoduleteardown ', ' _teardownpreviousclass ', ' _tests ', ' Addtest ', ' addtests ', ' counttestcases ', ' Debug ', ' Run '

Description

addtest (): The 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: (needed when organizing use cases)

[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __ module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' __weakref__ ', ' _makeresult ', ' buffer ', ' descriptions ', ' failfast ', ' resultclass ', ' run ', ' stream ', ' Verbosity ']

Description

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

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

Python unittest use case execution mode

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.