Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to modularize your py.test fixtures

Using py.test is great and the support for test fixtures is pretty awesome. However, in order to share your fixtures across your entire module, py.test suggests you define all your fixtures within one single conftest.py file. This is impractical if you have a large quantity of fixtures -- for better organization and readibility, you would much rather define your fixtures across multiple, well-named files. But how do you do that? ...No one on the internet seemed to know.

Turns out, however, you can define fixtures in individual files like this:

tests/fixtures/add.py

import pytest

@pytest.fixture
def add(x, y):
    x + y

Then you can import these fixtures in your conftest.py:

tests/conftest.py

import pytest
from fixtures.add import add

...and then you're good to test!

tests/adding_test.py

import pytest

@pytest.mark.usefixtures("add")
def test_adding(add):
    assert add(2, 3) == 5

Because of the modularity, tests will have to be run with python -m py.test instead of py.test directly.

@delaurentum

This comment has been minimized.

delaurentum commented Sep 21, 2017

thanks

@AndrewBMartin

This comment has been minimized.

AndrewBMartin commented Oct 17, 2017

I'm having trouble making this work when running multiple tests at the same time. Would appreciate any insight you might have. Please see the SO question.

Thank you

@boxysean

This comment has been minimized.

boxysean commented Oct 26, 2017

thank you

@ikonst

This comment has been minimized.

ikonst commented Mar 6, 2018

You can simply create "local pytest plugins" which can be nothing more than Python files with fixtures, e.g.

  • tests/unit/conftest.py:

    pytest_plugins = [
       "tests.unit.fixtures.some_stuff",
    ]
  • tests/unit/fixtures/some_stuff.py:

    import pytest
    
    @pytest.fixture
    def foo():
        return 'foobar'
@ryanjdillon

This comment has been minimized.

ryanjdillon commented Oct 11, 2018

Awesome ikonst!

As a note that the respective directories referred to in tests.unit.fixtures.some_stuff" need to have __init__.py files for the plugins to be loaded by pytest.

https://docs.pytest.org/en/documentation-restructure/how-to/writing_plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment