Skip to content

How to make testing utilities importable when using importlib mode #8964

@nicoddemus

Description

@nicoddemus

The --import-mode=importlib mode does not change sys.path in order to import test modules and conftest.py files, which has many benefits but also some drawbacks.

Often in test suites there are functions and classes used only for testing. This is not a problem when the tests are embedded in the source, but for layouts where the tests are in a separate directory, users don't have many options where to put those testing-only functions/classes in a way that is importable when using --import-mode=importlib (see #7245 (comment)).

One solution might be to have a new option that appends one or more directories to sys.path when running the tests only:

[pytest]
addopts = --import-mode=importlib
sys_path_append = tests

This at first might seem similar to just going ahead and using --import-mode=append, however this has the advantage that any directory can be added, instead of sys.path being implicitly changed just because tests/conftest.py exists with --import-mode=append.

This is just an idea, opening this issue to discuss the problem of how can we import testing utilities when using imporlib mode in layouts where tests are outside the source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions