If you are like me, that want's to have quick recipes off all possible variation in once for later use or just a refresher, here you have it.
So, there are 2 main ways in Pytest to setup/teardown test (without including unittest intgration, that is, when you actually using unittest)
1) Fixture (the pytest way )
2) xunit-style (similar to unittest)
These recipes can be found in my Programming-CookBook here -> PyTest Recipes
Fixture
Collection of fixture setup/teardown
import pytest
counter = 0
def add_counter():
global counter
counter += 1
# ---------------------------
# yield fixtures (recommended)
# ---------------------------
@pytest.fixture
def get_counter():
print(f"{counter}) -- Fixture (yield)")
add_counter()
yield counter
def test_count_is_1(get_counter):
assert get_counter == 1
def test_count_is_2(get_counter):
assert get_counter == 2
# ---------------------------
# Adding finalizers directly
# ---------------------------
@pytest.fixture
def get_counter_direct(request):
print(f"{counter}) -- Fixture (request)")
add_counter()
request.addfinalizer(add_counter)
return counter
def test_count_is_3(get_counter_direct):
assert get_counter_direct == 3
which results in
================================================================================================================ PASSES ================================================================================================================
___________________________________________________________________________________________________________ test_count_is_1 ____________________________________________________________________________________________________________
-------------------------------------------------------------------------------------------------------- Captured stdout setup ---------------------------------------------------------------------------------------------------------
0) -- Fixture (yield)
___________________________________________________________________________________________________________ test_count_is_2 ____________________________________________________________________________________________________________
-------------------------------------------------------------------------------------------------------- Captured stdout setup ---------------------------------------------------------------------------------------------------------
1) -- Fixture (yield)
___________________________________________________________________________________________________________ test_count_is_3 ____________________________________________________________________________________________________________
-------------------------------------------------------------------------------------------------------- Captured stdout setup ---------------------------------------------------------------------------------------------------------
2) -- Fixture (request)
========================================================================================================== 3 passed in 0.01s ==========================================================================================================
xunit-style
These are all collection of xunit-style like setup/teardown ways. Here I added a counter so that the order is visible
import pytest
counter = 0
def add_counter():
global counter
counter += 1
# ---------------------------
# Module Level Setup/TearDown
# ---------------------------
def setup_module(module):
"""setup any state specific to the execution of the given module."""
add_counter()
print(f"{counter}) -- SetUp (module)")
def teardown_module(module):
"""teardown any state that was previously setup with a setup_module method."""
add_counter()
print(f"{counter}) -- tearDown (module)")
class TestSomeClass:
# ---------------------------
# Class level setup/teardown
# ---------------------------
@classmethod
def setup_class(cls):
"""setup any state specific to the execution of the given class (which usually contains tests)."""
add_counter()
print(f"{counter}) -- SetUp (class)")
@classmethod
def teardown_class(cls):
"""teardown any state that was previously setup with a call to setup_class. """
add_counter()
print(f"{counter}) -- tearDown (class)")
# ---------------------------
# Class level setup/teardown
# ---------------------------
def setup_method(self, method):
"""setup any state tied to the execution of the given method in a class. setup_method is invoked for every test method of a class."""
add_counter()
print(f"{counter}) -- SetUp (method)")
def teardown_method(self, method):
"""teardown any state that was previously setup with a setup_method call. """
add_counter()
print(f"{counter}) -- tearDown (method)")
def test_is_1_a_number(self):
assert (1).__class__ is int
# ---------------------------
# module level setup/teardown
# ---------------------------
def setup_function(function):
"""setup any state tied to the execution of the given function. Invoked for every test function in the module."""
add_counter()
print(f"{counter}) -- SetUp (function)")
def teardown_function(function):
"""teardown any state that was previously setup with a setup_function call."""
add_counter()
print(f"{counter}) -- tearDown (function)")
def test_something_at_module_level():
assert (1).__class__ is int
which results in
================================================================================================================ PASSES ================================================================================================================
___________________________________________________________________________________________________ TestSomeClass.test_is_1_a_number ___________________________________________________________________________________________________
-------------------------------------------------------------------------------------------------------- Captured stdout setup ---------------------------------------------------------------------------------------------------------
1) -- SetUp (module)
2) -- SetUp (class)
3) -- SetUp (method)
------------------------------------------------------------------------------------------------------- Captured stdout teardown -------------------------------------------------------------------------------------------------------
4) -- tearDown (method)
5) -- tearDown (class)
____________________________________________________________________________________________________ test_something_at_module_level ____________________________________________________________________________________________________
-------------------------------------------------------------------------------------------------------- Captured stdout setup ---------------------------------------------------------------------------------------------------------
6) -- SetUp (function)
------------------------------------------------------------------------------------------------------- Captured stdout teardown -------------------------------------------------------------------------------------------------------
7) -- tearDown (function)
8) -- tearDown (module)
========================================================================================================== 2 passed in 0.01s ===========================================================================================================
Documentaiton