Skip to content

foo.test_foo is imported twice, including once before running conftest, when using pytest --pyargs foo.test_foo #5078

@anntzer

Description

@anntzer

Thanks for submitting an issue!

Here's a quick checklist in what to include:

  • Include a detailed description of the bug or suggestion
  • pip list of the virtual environment you are using
  • pytest and operating system versions
  • Minimal example if possible

foo.test_foo is imported twice, including once before running conftest, when using pytest --pyargs foo.test_foo.

Python 3.7 / Arch Linux

$ pip list
Package        Version
-------------- -------
atomicwrites   1.3.0  
attrs          19.1.0 
more-itertools 7.0.0  
pip            19.0.3 
pluggy         0.9.0  
py             1.8.0  
pytest         4.4.0  
setuptools     40.8.0 
six            1.12.0
$ tree
.
└── foo
    ├── conftest.py
    ├── __init__.py
    └── test_foo.py
$ cat foo/conftest.py
def pytest_configure(config):
    print("configuring")
$ cat foo/test_foo.py
print("in test_foo")
def test_1(): pass
$ pytest --pyargs foo.test_foo -s
===================================== test session starts ======================================
platform linux -- Python 3.7.3, pytest-4.4.0, py-1.8.0, pluggy-0.9.0
rootdir: /tmp/foo
collecting ... in test_foo
configuring
in test_foo
collected 1 item                                                                               

foo/test_foo.py .

=================================== 1 passed in 0.01 seconds ===================================

Note that "in test_foo" appears twice, including once before the call to pytest_configure (which prints out "configuring").

This means that if test_foo has toplevel logic that depends on pytest_configure (e.g., via https://docs.pytest.org/en/latest/example/simple.html#detect-if-running-from-within-a-pytest-run -- an example is matplotlib's test helpers, which has a decorator to either generate a pytest test or a nose test instance), the logic will be wrong -- even though the test collected "pre-configure" will actually be discarded later (this happens in collect_one_node in _perform_collect), this can still e.g. trigger spurious warnings from toplevel code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: collectionrelated to the collection phasetopic: rewriterelated to the assertion rewrite mechanismtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions