pytest-dev / pytest-dev/pytest

Add mode to only autodiscover paths with pyproject.toml or setup.py

Open
#12,016 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What's the problem this feature will solve?

There is no easy way to only discover the paths that actually contain a valid python package and only run tests in those directories (or subdirectories).

Imagine you have a monorepo that mixes python and other languages all under the packages directory (and subdirectories). There are other places where random python tests might exists for validation/ci purposes but should not be run when running pytest in the root directory. It will be anoying to specify every module you want to be included during collection since this is a monorepo and more packages could be added at any time.

Describe the solution you'd like

I would like a .ini option something like only_discover_packages = true which will discover only those paths that contain a valid python package (one that contains a pyproject.toml or setup.py), then that path will be added both to pythonpath and testpaths.

Alternative Solutions

The easiest way I found to achieve is by doing something like this in my pyproject.toml:

[tool.pytest.ini_options]
addopts = "--import-mode=importlib"
testpaths = [
    "packages/pkg1",
    "packages/pkg3"
]
pythonpath = [
    "packages/pkg1",
    "packages/pkg3"  # Let's say pkg2 only has C++ code so we skip it
]

alternatively a different option could be added that only looks in subdirectories which contain a valid pytest configuration file (ie: pytest.ini, pyproject.toml...) and collects tests in those.

If needed I could try and write a PR though I am not sure I have the time nor am I familiar with the pytest codebase.

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 by reviewing pytest's existing configuration and test collection behavior, especially pyproject.toml options such as testpaths and pythonpath. Compare discovery under directories containing pyproject.toml or setup.py with unrelated Python test directories. Done means a documented option consistently limits discovery and updates both collection paths and import paths as requested.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.