pytest-dev / pytest-dev/pytest

Explicit control of fixtures outside of test run

Open
#3,817 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Pytest fixtures have some really powerful features/properties:

  • inversion of control (plugins add new functionality)
  • dependency injection (fixtures declare their dependencies by fixture name)
  • multiple fixtures can depend on shared funcitonality.

When applying pytest fixtures to various services, this creates a powerful environment for orchestrating those services.

In one of our test suites, we have a more complicated form of the following fixtures:

@pytest.fixture(scope='session')
def api_service(mongodb_instance, postgres_instance, other_service):
    ...
    return ApiServiceDescription(...)

@pytest.fixture(scope='session')
def other_service(postgres_instance):
    ...
    return OtherServiceDescription(...)

These fixtures have some really nice properties that I would like to use outside running tests, in other phases of development, such as a development instance of the project under test. It would be nice to be able to start up api_service from the command line or Python script, let pytest deal with the dependencies, return control to the user (or sleep) for as long as needed, and clean up after.

Most importantly, I'd like to have one place where these fixtures (and their dependencies and startup/teardown logic) are defined, even if they're orchestrated in two environments (running tests and other manual invocations).

The most obvious way I can see to accomplish this would be for pytest to present an interface for 'running' fixtures. Any other approach I consider (using Docker compose, writing another orchestration framework) requires a lot of duplication of the functionality in the fixtures.

Is it possible to do this with the code as currently presented, maybe with just a few lines of code? I found the FixtureManager, which requires a Session which requires a Config object which requires a PluginManager. Can you suggest a way that someone might be able to hack together a fixture runner? Would you consider a feature to expose such functionality more simply, such that one could run something like:

$ pytest --run-fixture api_service

or maybe

$ python -m pytest.run-fixtures api_service

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/_pytest/fixtures.py at FixtureManager and trace the Session, Config, and PluginManager setup described in the issue. Compare that internal path with the proposed --run-fixture api_service or python -m pytest.run-fixtures entry point. Done would require a supported way to run a named fixture, resolve dependencies, preserve teardown, and return control to the caller.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.