The pytest of the Python Unit test framework--fixtures

Source: Internet
Author: User

Fixtures not very good translation, can be regarded as a sandwich biscuit the outermost two pieces of biscuits. Usually expressed in Setup/teardown . It is mainly used to wrap test cases, why do you need such cookies? We take Web Automation testing as an example, for example, a system to be tested requires login / exit. Then each use case needs to log in before execution, and all need to exit, so that each use case repeatedly write login and exit is cumbersome, of course, you can also encapsulate login and exit as a method call, but each use case write calls is also very troublesome. It's a lot easier with fixtures.

Test function

Create a test_fixtures.py file

#Coding=utf-8Importpytest#function FunctiondefMultiply (A, b):returnAb#=====fixtures========defSetup_module (module):Print("\ n")    Print("setup_module================>")defTeardown_module (module):Print("teardown_module=============>")defsetup_function (function):Print("setup_function------>")defteardown_function (function):Print("teardown_function--->")#===== test Case ========deftest_numbers_3_4 ():Print 'Test_numbers_3_4'    assertMultiply (3,4) = = 12deftest_strings_a_3 ():Print 'Test_strings_a_3'    assertMultiply'a', 3) = ='AAA' if __name__=='__main__': Pytest.main ("- s test_fixtures.py")

Operation Result:

============================= test session starts =============================--Python 2.7.10--py-1.4.30- -pytest-2.7.22 itemstest_fixtures.py setup_module================>setup_function-- ---->test_numbers_3_4.teardown_function--->setup_function------>Test_strings_a_ 3.teardown_function--->teardown_module in 0.01 seconds ===========================

By executing the results, it is easy to figure out the order in which they are executed.

Setup_module/teardown_module executes after all test cases are executed and after.

Setup_function/teardown_function is executed after and after each test case.

Test class

#Coding=utf-8Importpytest#function FunctiondefMultiply (A, b):returnAbclasstestum:#=====fixtures========    defSetup (self):Print("Setup----->")    defteardown (self):Print("teardown-->")    defSetup_class (CLS):Print("\ n")        Print("setup_class=========>")    defTeardown_class (CLS):Print("teardown_class=========>")    defSetup_method (Self, method):Print("Setup_method----->>")    defTeardown_method (Self, method):Print("teardown_method-->>")        #===== test Case ========    defTest_numbers_5_6 (self):Print 'Test_numbers_5_6'        assertMultiply (5,6) = = 30deftest_strings_b_2 (self):Print 'test_strings_b_2'        assertMultiply'b', 2) = ='BB'if __name__=='__main__':p Ytest.main ("- s test_fixtures.py")

Operation Result:

============================= test Session starts =============================Platform Win32--Python 2.7.10--py-1.4.30--pytest-2.7.2Rootdir:d:\pyse\pytest, inifile:plugins:htmlcollected2itemstest_fixtures.py Setup_class=========>Setup_method----->>Setup----->Test_numbers_5_6.teardown-Teardown_method-->>Setup_method----->>Setup----->Test_strings_b_2.teardown-Teardown_method-->>Teardown_class=========>========================== 2 passedinch0.00 Seconds ===========================

The Setup_class/teardown_class is executed at the beginning and end of the current Test class.

The Setup/treadown is executed at the beginning and end of each test method.

Setup_method/teardown_method is executed at the beginning and end of each test method, the same as the Setup/treadown level.

The pytest of the Python Unit test framework--fixtures

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.