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-8import pytest# function def multiply (A, B): return a * b# =====fixtures========def setup_module (module): Print ( "\ n") print ("setup_module================>") def teardown_module (module): print ("Teardown_ Module=============> ") def setup_function (function): print (" setup_function------> ") def teardown_ Function: print ("Teardown_function--->") # ===== test Case ========def test_numbers_3_4 (): print ' Test_numbers_3_4 ' assert multiply (3,4) = = Def test_strings_a_3 (): print ' test_strings_a_3 ' assert Multiply (' A ', 3) = = ' AAA ' if __name__ = = ' __main__ ': pytest.main ("-S test_fixtures.py")
Operation Result:
============================= test Session starts =============================platform Win32--Python 2.7.10--py-1.4 .--Pytest-2.7.2rootdir:d:\pyse\pytest, inifile:plugins:htmlcollected 2 itemstest_fixtures.py setup_module======= =========>setup_function------>test_numbers_3_4.teardown_function--->setup_function------>test_ Strings_a_3.teardown_function--->teardown_module=============>========================== 2 passed 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-8import pytest# function def multiply (A, B): return a * bclass testum: # =====fixtures======== def Setup: print ("Setup----->") def teardown (self): print ("teardown-->") def Setup_class (CLS): print ( "\ n") print ("setup_class=========>") def Teardown_class (CLS): print ("teardown _class=========> ") def setup_method (Self, method): print (" Setup_method----->> ") def Teardown_method (Self, method): print ("teardown_method-->>") # ===== test Case ======== def test_ Numbers_5_6 (self): print ' test_numbers_5_6 ' assert multiply (5,6) = = def test_strings_b_2 (self): print ' test_strings_b_2 ' assert multiply (' 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 .--Pytest-2.7.2rootdir:d:\pyse\pytest, inifile:plugins:htmlcollected 2 itemstest_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 passed in 0.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.
Pytest of the Python Unit test framework--fixtures