stevew
stevew

Reputation: 734

Structure of importable pytest plugin

I have some data/fixtures that are common across several projects. So the sensible thing to do is to refactor them into a standalone project and import them in the projects. Structure of the project I'm about to refactor looks like this:

my-project/
    src/
        db/
            __init__.py
            module1.py
    tests/
        data/
            test_data.csv
        test_module1.py

test_module1.py contains both the fixtures to be refactored and the unit tests for module1.py. The official way to share fixtures is via pytest-plugins. The official documentation provides the following pyproject.toml example:

# sample ./pyproject.toml file
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "myproject"
classifiers = [
    "Framework :: Pytest",
]

[tool.setuptools]
packages = ["myproject"]

[project.entry_points]
pytest11 = [
    "myproject = myproject.pluginmodule",
]

My questions are:

  1. Do I refactor the fixtures and data from tests/ into new-fixture-project/src/test_data/ (like a regular package)? Or new-fixture-project/tests/?
  2. In either case, what should the entry point be? Is it new-fixture-project = test_data.data (suppose I've refactored the fixtures into test_data/data.py?

[Edit] Answer to my question in the comment, this is what you need to build it as a regular package.

In pyproject.toml

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

In setup.cfg, add this section

[options.entry_points]
pytest11 =
    test_data = test_data.plugin

Upvotes: 0

Views: 692

Answers (1)

Peter K
Peter K

Reputation: 2484

I created an open-source python project you can look into for reference: pytest-mock-generator.

Instead of src directory, the project is structured like so:

new-fixture-project/
    new_fixture_project/
        db/
            __init__.py
            module1.py
    tests/
        data/
            test_data.csv
        test_module1.py

So to your questions - the fixtures and code go to new-fixture-project/new_fixture_project/.

The entry point is new_fixture_project, which is the module name (instead of src), if you define your fixtures in the root __init__.py for simplicity. You can also define your fixtures in module1.py for example, then the entry point would be new_fixture_project.module1.

Few useful links from my project:

Upvotes: 1

Related Questions