Fixture

Test fixture is like Depndency Injection for pytest. It will be recall each time you use it

For example considering this situation

from src.services.MyService import MyService
from src.services.Database import Database

class TestMyService:
    def setup_method(self, method):
        self.app = MyService(Database())

    def teardown_method(self, method):
        del self.app
        
    def test_load_user_should_success(self):
        self.app.load_user

The Database could be dependency injected. To do that we create a fixture.

Fixture way it will be like this

from src.services.MyService import MyService
from src.services.Database import Database
import pytest

@pytest.fixture
def database():
    return Database()

@pytest.fixture
def app(database):
    return MyService(database)

class TestMyService:
    def test_load_user_should_success(self, app):
        actual = app.load_user("Austin")
        assert actual is True

    def test_load_user_should_success_2(self, app):
        actual = app.load_user("Austin")
        assert actual is True

In this case, app fixture will be called twice.

If you want these fixtures to be available for other files, consider Conftest