I am working with a code base which which is mostly written with unittest. The tests are run with pytest. I am trying to simplify the tests by parameterizing some of them. To work towards this goal, I am trying to learn how to use pytest fixtures and refactor some of the existing unittest-based tests. The current tests have a global fixture in conftest.py:
@pytest.fixture(autouse=True)
def register_cleanup():
yield True
print("cleaning up database")
Now I want to add a fixture which is specific to one of my test modules, something along the lines of
@pytest.fixture()
def foo_fixture():
print("setup fixture")
yield
print("tear down fixture")
class Foo(unittest.TestCase):
def setUp(self):
print('unittest setUp()')
def test(self):
print('test')
However, the print()
statements in this module fixture never execute. As an intermediate step, I am keeping the unittest-based structure and adding to it. Is it possible to get this working the way I want or do I need to scrap unittest altogether and go directly to pytest.
autouse=True
here as well? In general, fixtures are only executed when they are a parameter to the test, and are less of generic cleanup mechanisms than scoped assumptions – Cireoautouse=True
tofoo_fixture()
similarly toregister_cleanup()
? – Code-Apprentice