When py.test is executed with file_or_dir arguments for:
- a directory and
- a file that
- is in the directory but
- does not match the configured Python test module pattern (e.g.,
test_*.py),
the file will be collected twice.
I believe this is because the file in question is considered for collection twice (once as a member of an explicitly-requested directory and again as an explicitly-requested file) and matches Session#isinitpath both times, thus being collected twice.
This is similar to #1187, except that the file would not ordinarily be collected as a member of the directory, since it does not match the Python test module pattern. As such, this is not a case of explicitly requesting collection of the same file_or_dir twice.
Example
Setup
$ python --version
Python 2.7.13
$ pip list
pip (9.0.1)
py (1.4.34)
pytest (3.2.1)
setuptools (28.8.0)
$ tree
.
└── tests
├── _test_three.py
├── _test_two.py
└── test_one.py
Scenario A: Default test collection
$ py.test --collect-only
===================================== test session starts =====================================
platform darwin -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: $HOME/src/pytest-duplicate-collection, inifile:
collected 1 item
<Module 'tests/test_one.py'>
<Function 'test_one'>
================================ no tests ran in 0.01 seconds =================================
Scenario B: Explicit directory test collection
$ py.test --collect-only tests
===================================== test session starts =====================================
platform darwin -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: $HOME/src/pytest-duplicate-collection, inifile:
collected 1 item
<Module 'tests/test_one.py'>
<Function 'test_one'>
================================ no tests ran in 0.01 seconds =================================
Scenario C: Explicit non-matching file test collection
$ py.test --collect-only tests/_test_two.py
===================================== test session starts =====================================
platform darwin -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: $HOME/src/pytest-duplicate-collection, inifile:
collected 1 item
<Module 'tests/_test_two.py'>
<Function 'test_two'>
================================ no tests ran in 0.00 seconds =================================
Scenario D: Explicit directory and non-matching file test collection
$ py.test --collect-only tests tests/_test_two.py
===================================== test session starts =====================================
platform darwin -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: $HOME/src/pytest-duplicate-collection, inifile:
collected 3 items
<Module 'tests/_test_two.py'>
<Function 'test_two'>
<Module 'tests/test_one.py'>
<Function 'test_one'>
<Module 'tests/_test_two.py'>
<Function 'test_two'>
================================ no tests ran in 0.01 seconds =================================
When
py.testis executed withfile_or_dirarguments for:test_*.py),the file will be collected twice.
I believe this is because the file in question is considered for collection twice (once as a member of an explicitly-requested directory and again as an explicitly-requested file) and matches
Session#isinitpathboth times, thus being collected twice.This is similar to #1187, except that the file would not ordinarily be collected as a member of the directory, since it does not match the Python test module pattern. As such, this is not a case of explicitly requesting collection of the same
file_or_dirtwice.Example
Setup
Scenario A: Default test collection
Scenario B: Explicit directory test collection
Scenario C: Explicit non-matching file test collection
Scenario D: Explicit directory and non-matching file test collection