Pytest Fixture providing value with teardown
examples/pytest/fixture-with-teardown/test_app.py
import pytest import application @pytest.fixture() def myapp(): print('myapp starts') app = application.App() yield app app.shutdown() print('myapp ends') def test_add_user_foo(myapp): myapp.add_user("Foo") assert myapp.get_user() == 'Foo' def test_add_user_bar(myapp): myapp.add_user("Bar") assert myapp.get_user() == 'Bar'
examples/pytest/fixture-with-teardown/application.py
class App: def __init__(self): self.pi = 3.14 # .. set up database print("__init__ of App") def shutdown(self): print("shutdown of App cleaning up database") def add_user(self, name): print("Working on add_user({})".format(name)) #self.name = name def get_user(self): return self.name
$ pytest -sq
getapp starts __init__ of App Working on add_user(Foo) shutdown of App cleaning up database getapp ends getapp starts __init__ of App Working on add_user(Bar) shutdown of App cleaning up database getapp ends