For each test item, test class, and entire test module (py file) with pytest How to write to perform pre-processing and post-processing.
Functions / methods to be used as targets for pre-processing / post-processing
--Each test function (test_ ~~
)
--setup_function (function)
and teardown_function (function)
--Each test method of the test class (test_ ~~
in the class)
--setup_method (self, method)
and teardown_method (self, method)
--Whole test class (class Test ~~
)
--setup_class (cls)
and teardown_class (cls)
@ classmethod
)
--Whole test module (whole .py file)
--setup_module (module)
and teardown_module (module)
When writing with xunit
def setup_module(module):
print("\n*** setup_module ***")
def teardown_module(moduloe):
print("\n*** teardown_module ***")
def setup_function(function):
print("\n=== setup_function ===")
def teardown_function(function):
print("\n=== teardown_function ===")
def test_test1():
print("test1")
def test_test2():
print("test2")
class TestCase:
@classmethod
def setup_class(cls):
print("\n@@@ setup_class @@@")
@classmethod
def teardown_class(cls):
print("\n@@@ teardown_class @@@")
def setup_method(self, method):
print("\n--- setup_method ---")
def teardown_method(self, method):
print("\n--- teardown_method ---")
def test_test3(self):
print("test3")
def test_test4(self):
print("test4")
Execution result
==================================== test session starts ====================================
platform linux -- Python 3.6.3, pytest-5.3.0, py-1.8.0, pluggy-0.13.1 -- /home/bskke1040/.pyenv/versions/3.6.3/bin/python3.6
cachedir: .pytest_cache
rootdir: /mnt/c/Users/bskke1040.KMJP/Clouds/OneDrive/study/pytest
collected 4 items
test_sample.py::test_test1
*** setup_module ***
=== setup_function ===
test1
PASSED
=== teardown_function ===
test_sample.py::test_test2
=== setup_function ===
test2
PASSED
=== teardown_function ===
test_sample.py::TestCase::test_test3
@@@ setup_class @@@
--- setup_method ---
test3
PASSED
--- teardown_method ---
test_sample.py::TestCase::test_test4
--- setup_method ---
test4
PASSED
--- teardown_method ---
@@@ teardown_class @@@
*** teardown_module ***
===================================== 4 passed in 0.05s =====================================
When writing with fixture
import pytest
@pytest.fixture(scope="module")
def my_setup_module(request):
print("\n######## my_setup_module ########")
def my_teardown_module():
print("\n######## my_teardown_module ########")
request.addfinalizer(my_teardown_module)
@pytest.fixture()
def my_setup_function(request):
print("\n--- my_setup_function ---")
def my_teardown_function():
print("\n--- my_teardown_function ---")
request.addfinalizer(my_teardown_function)
@pytest.fixture(scope="class")
def my_setup_class(request):
print("\n***** my_setup_class *****")
def my_teardown_class():
print("\n***** my_teardown_class *****")
request.addfinalizer(my_teardown_class)
@pytest.fixture()
def my_setup_method(request):
print("\n=== my_setup_method ===")
def my_teardown_method():
print("\n=== my_teardown_method ===")
request.addfinalizer(my_teardown_method)
def test_test1(my_setup_module, my_setup_function):
print("\ntest1")
def test_test2(my_setup_module, my_setup_function):
print("\ntest2")
class TestCase:
def test_test3(self, my_setup_module, my_setup_class, my_setup_method):
print("\ntest3")
def test_test4(self, my_setup_module, my_setup_class, my_setup_method):
print("\ntest4")
Recommended Posts