Skip to content

Configuration option to "skip collection and testing entirely" to help support Monorepos #13321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gregglind opened this issue Mar 21, 2025 · 5 comments

Comments

@gregglind
Copy link

What's the problem this feature will solve?

In monorepo situtations [1] there can be top-level pyproject.toml files and workspaces that have no tests, and do not want test collection, or where test collection generates errors.

Currently, when no tests are found, Pytest finishes with exit status 5, for good reasons. [2]

[1]: nx run-many test, etc.
[2]: #2393

Describe the solution you'd like

An ini-able flag to disable all Pytest actions (test collection, test running, all of it), called --do-nothing, or --disable-pytest or --no-collect.

Alternative Solutions

  1. Create a new pytest-plugin for disable-pytest
  2. (Adequate) Use existing Plugin + Config
    a. Pytest plugin to change the exit code: https://pypi.org/project/pytest-custom-exit-code/
    b. norecursedirs =["*"], to ensure 0 tests are found
  3. Creating a 'fake test' directory, and point discovery only there. This seems strange for top-level monorepo with no code.
  4. Avoid situations where pytest is called a project or folder where this is relevant.

Additional context

  1. Naming: If this flag or plug-in existed, what would be a good name for it?
  • Bad: "skip", "succeed" "exit" flags have test related meanings.
  • Better? --disable-pytest, --do-nothing , --no-collect?
  1. Behaviour: would should happen if a downstream project had this set? I assume it would disable collection under that project only.
@The-Compiler
Copy link
Member

I don't think this is something pytest needs to solve. If you don't want to run pytest, just don't run pytest? I'm not aware of what nx run-many test does precisely, but surely a

Build system, optimized for monorepos, with AI-powered architectural awareness and advanced CI capabilities.

has a way to just not invoke pytest if that is undesirable for the given project/subfolder?

@RonnyPfannschmidt
Copy link
Member

The usecase is unclear

The recommendation woul be to invoke pytest per project subfolder instead of doing a vatch all

Id expect different subprojects to need different environment preparation, pytest plugins and possible local configurations

So why is there a need to just run pytest randomly on all the things

@gregglind
Copy link
Author

Thanks for the responses so far.


To this:

has a way to just not invoke pytest if that is undesirable for the given project/subfolder?

I would like that way to come from pyproject.toml itself. That's is the short version of the ask.


To this:

Id expect different subprojects to need different environment preparation, pytest plugins and possible local configurations

That part is handled effectively by pyproject.toml in each project.


I also haven't found any way to short circuit the process from inside a conftest.py.

This would also work:

@pytest.hookimpl(tryfirst=True)
def pytest_sessionstart(session: Session):
    (something something)...  pytest_sessionfinish(session=session, exitstatus=ExitCode.OK)

@Zac-HD
Copy link
Member

Zac-HD commented Mar 23, 2025

I suggest using a shell alias, so that you can run whatever logic you want to about whether to run pytest at all outside of Pytest.

@Zac-HD Zac-HD closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2025
@gregglind
Copy link
Author

gregglind commented Mar 25, 2025

For any future testers, this was my workaround solution:

  1. uv add --dev pytest-custom_exit_code

  2. Configure pyproject.toml

     [tool.pytest.ini_options]
     
     # Exclude all directories from discovery
     norecursedirs =["*"]
     
     # Change the exit code for "No tests were collected" from 5 to 0, 
     #      via `pytest-custom_exit_code` plugin
     addopts=['--suppress-no-test-exit-code']
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants