Search code examples
pythonunit-testingpytestfixtures

Reuse pytest fixtures


I am writing some tests using pytest many of which have similar fixtures. I want to put these "global" fixtures in a single file so that they can be reused across multiple test files. My first thought was to create a fixtures.py file such as

import pytest


@pytest.fixture()
def my_fixture():
    # do something

Now how do I use this fixture in my_tests.py?

def test_connect(my_fixture):
    pass

This gives fixture 'my_fixture' not found. I can from fixtures import my_fixture. What is the suggested solution to this situation?


Solution

  • Pytest will share the fixtures in conftest.py automatically. Move the shared fixture from fixtures.py to conftest.py.