Splitting a conftest.py file into several smaller conftest-like parts

pytest, python

Solution

You can put your stuff in other modules and reference them using a pytest_plugins variable in your `conftest.py`.

In the file `tests/unit/conftest.py`:

  pytest_plugins = [
     "tests.unit.fixtures.some_stuff",
  ]

In the file: `tests/unit/fixtures/some_stuff.py`:

  import pytest

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

This will also work if your `conftest.py` has hooks on them.

Problem

I've got a large conftest.py file that I wish to split into smaller parts, for two reasons: - The file is very large (~1000 lines, including documentation) - Some of the fixtures depend on other fixtures, and I have no reason to expose those other fixtures as part of the conftest "API" when users look for relevant fixtures I am not aware of any mechanism provided by pytest to resolve conftest files in multiple locations within the same folder, so I contrived one, below: ``` import sys import os sys.path.append(os.path.dirname(__file__)) from _conftest_private_part_1 import * from _conftest_private_part_2 import * from _conftest_private_part_3 import * @pytest.fixture def a_fixture_that_is_part_of_the_public_conftest_api(): pass ``` This works for my needs, but I do wonder if there is a better way.

Original source