'' ' function level (setup_function / teardown_function only function with the embodiment of Health effect (not in a class) in the class is the process does not take effect ' '' Import pytest DEF setup_module (): "" " This is a module level setup it will be in this module (test_fixt_function.py) in all previous test execution, it is called once. Note that it is defined as a direct function module in "" " Print () Print ( " -------- Setup the before module -------------- ------ " ) DEF teardown_module (): " "" this is a module-level teardown, it will be in this module (test_fixt_function.py ) years after completion of all test execution, it is called once. Note that it is defined as a direct function module in "" " print("After tearDown Module1 -------------- -------------- " ) DEF setup_function (): Print () Print ( " setup_function: before each use case begin " ) DEF teardown_function (): Print ( " teardown_function: not a begin after the use case " ) DEF test_one (): Print ( " executing test_one " ) X = " Hello Word " Assert " H " in X DEF test_two ( ): Print("正在执行test_two") a = "hello" b = "hello word" assert a in b if __name__ == '__main__': pytest.main(['-s','test_fixt_function.py'])
operation result:
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.1.0, py-1.8.0, pluggy-0.12.0
rootdir: E:\py_pytest\interfacecollected 2 items
test_fixt_function.py
-------------- setup before module --------------
setup_function: Each started with cases before
being executed test_one.
teardown_function: not started after a use case
setup_function: Each started with cases before
being executed test_two.
teardown_function: not a use case started after
-------------- teardown after module ------------- -
[100%]
============================== 2 passed in 0.02s ==============================