Skip to content

pytest collects files when told not to #11006

@CobaltCause

Description

@CobaltCause

I can reproduce this with a project set up as follows:

  • pyproject.toml:

    [tool.pytest.ini_options]
    python_files = "*.py"
    testpaths = [
        "tests",
    ]
  • src/playground/__init__.py exists and is empty

  • src/playground/__main__.py:

    import sys
    
    
    def main():
        print("Hello, world!")
    
    
    sys.exit(main())
  • tests does not exist

When I run pytest, I get this output:

============================= test session starts ==============================
platform linux -- Python 3.11.3, pytest-7.2.1, pluggy-1.0.0
rootdir: /home/charles/science/python/playground, configfile: pyproject.toml
plugins: cov-4.0.0
collected 0 items / 1 error

==================================== ERRORS ====================================
_________________ ERROR collecting src/playground/__main__.py __________________
src/playground/__main__.py:11: in <module>
    sys.exit(main())
E   SystemExit
------------------------------- Captured stdout --------------------------------
Hello, world!
=========================== short test summary info ============================
ERROR src/playground/__main__.py - SystemExit
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.03s ===============================

I would expect this to succesfully collect nothing and run no tests, but instead it fails because it runs __main__.py. Notably, if I remove python_files = "*.py" from pyproject.toml, it works. It seems like this shouldn't matter, though, because I'm already narrowing testpaths to tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: collectionrelated to the collection phasetopic: configrelated to config handling, argument parsing and config file

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions