Pytest Fixture providing value



examples/pytest/fixture-with-value/test_app.py
import pytest
import application


@pytest.fixture()
def app():
    print('app starts')
    myapp = application.App()
    return myapp


def test_add_user_foo(app):
    app.add_user("Foo")
    assert app.get_user() == 'Foo'

def test_add_user_bar(app):
    app.add_user("Bar")
    assert app.get_user() == 'Bar'

examples/pytest/fixture-with-value/application.py
class App:
    def __init__(self):
        self.pi = 3.14
        # .. set up database
        print("__init__ of App")


    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)

getapp starts
__init__ of App
Working on add_user(Bar)