pytest use case runtime level:
Module level (setup_module/teardown_module) starts at the beginning and ends of the module, and is global (out-of-class, in-function)
Function level (setup_function/teardown_function) is only valid for function use cases (when there are functions outside the class)
The class level (setup_class/teardown_class) runs only once before and after the class (valid in the class, not outside the class)
Method level (setup_method/teardown_method) begins at the beginning and end of the method (in class, not outside class)
setup/teardown in a class runs before and after invoking a method (valid in a class, valid when a function outside a class)
Functional level:
Function pre-set up_function and post-teardown_function are called once before and after each use case starts (function level, multiple calls)
import pytest def setup_function(): print("setup_function:Each use case is executed before it starts") def teardown_function(): print("teardown_function:Each use case is executed at the end of the use case") def test_one(): print("Executing---test_one") def test_two(): print("Executing---test_two") def login(): print("Executing---test_two") if __name__ == "__main__": pytest.main(["-s","test02.py"]) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test02.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\Test collected 2 items test02.py setup_function:Each use case is executed before it starts //Executing - test_one .teardown_function:Each use case is executed at the end of the use case setup_function:Each use case is executed before it starts //Executing - test_two .teardown_function:Each use case is executed at the end of the use case ============================== 2 passed in 0.14s ============================== Process finished with exit code 0
Module pre-setup_module and post-teardown_module are invoked once before and after the whole. py module starts (module level, only once)
import pytest def setup_module(): print("setup_module:Whole.py Module starts executing once") def teardown_module(): print("teardown_module:Whole.py Module End Execution Once") def setup_function(): print("setup_function:Each use case is executed before it starts") def teardown_function(): print("teardown_function:Each use case is executed at the end of the use case") def test_one(): print("Executing---test_one") def test_two(): print("Executing---test_two") def login_test(): print("Executing---test_two") if __name__ == "__main__": pytest.main(["-s","test02.py"]) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test02.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\Test collected 2 items test02.py setup_module:Whole.py Module starts executing once setup_function:Each use case is executed before it starts //Executing - test_one .teardown_function:Each use case is executed at the end of the use case setup_function:Each use case is executed before it starts //Executing - test_two .teardown_function:Each use case is executed at the end of the use case teardown_module:Whole.py Module End Execution Once ============================== 2 passed in 0.08s ============================== Process finished with exit code 0
Class level:
Class pre-setup_class and post-teardown_class are invoked once before and after the beginning of all use cases (class level, only once)
Method pre-setup_method and post-teardown_method are invoked once before and after the beginning of each use case (method level, multiple invocations)
In the class, the pre-set up and post-teardown are invoked once before and after the beginning of each use case (in the method, before and after the method is invoked, many times)
setup_class: Before all use cases are executed (class level)
setup_method: Execution before each use case starts (method level)
setup: Execute each use case before it starts (before calling a method)
Executing - test_one
teardown: Execute after each use case (after calling the method)
teardown_method: Execution at the end of each use case (method level)
setup_method: Execution before each use case starts (method level)
setup: Execute each use case before it starts (before calling a method)
Executing - test_two
teardown: Execute after each use case (after calling the method)
teardown_method: Execution at the end of each use case (method level)
teardown_class: After all use cases are executed (class level)
import pytest class Test(): def setup(self): print("setup: Execute each use case before it starts (before calling a method)") def teardown(self): print("teardown: Execution after each use case (after method invocation)") def setup_class(self): print("setup_class:Before all use cases are executed (class level)") def teardown_class(self): print("teardown_class:After all use cases are executed (class level)") def setup_method(self): print("setup_method:Execution before each use case starts (method level)") def teardown_method(self): print("teardown_method:Execution at the end of each use case (method level)") def test_one(self): print("Executing---test_one") def test_two(self): print("Executing---test_two") def login_test(self): print("Executing---test_two") if __name__ == "__main__": pytest.main(["-s","test02.py"]) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test02.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\Test collected 2 items test02.py setup_class:Before all use cases are executed (class level) setup_method:Execution before each use case starts (method level) setup: Execute each use case before it starts (before calling a method) //Executing - test_one .teardown: Execution after each use case (after method invocation) teardown_method:Execution at the end of each use case (method level) setup_method:Execution before each use case starts (method level) setup: Execute each use case before it starts (before calling a method) //Executing - test_two .teardown: Execution after each use case (after method invocation) teardown_method:Execution at the end of each use case (method level) teardown_class:After all use cases are executed (class level) ============================== 2 passed in 0.21s ============================== Process finished with exit code 0
Class and function mix:
setup_module and teardown_module have the highest priority
import pytest def setup_module(): print("-setup_module:Whole.py Modules begin to execute once [function]") def teardown_module(): print("-teardown_module:Whole.py Module End Execution Once [Function]") def setup_function(): print("====setup_function:Every use case executes [functions] before it starts.") def teardown_function(): print("====teardown_function:The function is executed after each use case is completed.") def test_one(): print("Executing---test_one[Function)") def test_two(): print("Executing---test_two[Function)") def setup(): print("@@@@@setup: Execution before each use case starts (before calling a method) [Function]") def teardown(): print("@@@@@teardown: Execution after each use case (after calling method) [function]") class Test(): def setup(self): print("setup: Execute each use case before it starts (before calling a method)") def teardown(self): print("teardown: Execution after each use case (after method invocation)") def setup_class(self): print("setup_class:Before all use cases are executed (class level)") def teardown_class(self): print("teardown_class:After all use cases are executed (class level)") def setup_method(self): print("setup_method:Execution before each use case starts (method level)") def teardown_method(self): print("teardown_method:Execution at the end of each use case (method level)") def test_three(self): print("Executing---test_one") def test_four(self): print("Executing---test_two") if __name__ == "__main__": pytest.main(["-s","test02.py"]) "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test02.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\Test collected 4 items test02.py -setup_module:Whole.py Modules begin to execute once [function] ====setup_function:Every use case executes [functions] before it starts. @@@@@setup: Execution before each use case starts (before calling a method) [Function] //Executing - test_one [function] .@@@@@teardown: Execution after each use case (after calling method) [function] ====teardown_function:The function is executed after each use case is completed. ====setup_function:Every use case executes [functions] before it starts. @@@@@setup: Execution before each use case starts (before calling a method) [Function] //Executing - test_two [function] .@@@@@teardown: Execution after each use case (after calling method) [function] ====teardown_function:The function is executed after each use case is completed. setup_class:Before all use cases are executed (class level) setup_method:Execution before each use case starts (method level) setup: Execute each use case before it starts (before calling a method) //Executing - test_one .teardown: Execution after each use case (after method invocation) teardown_method:Execution at the end of each use case (method level) setup_method:Execution before each use case starts (method level) setup: Execute each use case before it starts (before calling a method) //Executing - test_two .teardown: Execution after each use case (after method invocation) teardown_method:Execution at the end of each use case (method level) teardown_class:After all use cases are executed (class level) -teardown_module:Whole.py Module End Execution Once [Function] ============================== 4 passed in 0.04s ============================== Process finished with exit code 0