pytest: `--deselect` doesn't work with absolute paths

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

I wanted to do something like pytest /path/to/project/tests/* --deselect /path/to/project/tests/test_needsToBeAvoided.py, but this didn’t work. Instead I had to do pytest /path/to/project/tests/* --deselect tests/test_needsToBeAvoided.py.

bstaletic@Gallifrey pytest % pip list
Package               Version
--------------------- ----------
attrs                 19.3.0
beautifulsoup4        4.8.2
certifi               2019.11.28
chardet               3.0.4
codecov               2.0.15
coverage              5.0.3
entrypoints           0.3
flake8                3.7.9
flake8-comprehensions 3.2.2
flake8-ycm            0.1.0
idna                  2.8
mccabe                0.6.1
more-itertools        8.2.0
packaging             20.1
pip                   20.0.2
pluggy                0.13.1
psutil                5.6.7
ptvsd                 4.3.2
py                    1.8.1
pycodestyle           2.5.0
pyflakes              2.1.1
PyHamcrest            2.0.0
pyparsing             2.4.6
pytest                5.3.5
pytest-cov            2.8.1
requests              2.22.0
setuptools            41.2.0
six                   1.14.0
soupsieve             1.9.5
urllib3               1.25.8
waitress              1.4.3
wcwidth               0.1.8
WebOb                 1.8.6
WebTest               2.0.34
bstaletic@Gallifrey pytest % python -c 'print(__import__("pytest").__version__)'
5.3.5
bstaletic@Gallifrey pytest % cat /etc/os-release
NAME="Artix Linux"
PRETTY_NAME="Artix Linux"
ID=artix
BUILD_ID=rolling
ANSI_COLOR="0;36"
HOME_URL="https://www.artixlinux.org/"
DOCUMENTATION_URL="https://wiki.artixlinux.org/"
SUPPORT_URL="https://forum.artixlinux.org/"
BUG_REPORT_URL="https://gitea.artixlinux.org/"
LOGO=artixlinux

Steps to repro:

  • mkdir ~/test
  • cd ~/test
  • echo 'def test_test():assert 1' > test_test.py
  • pytest ~/test/*_test.py --deselect test_test.py - this works. The report says “1 test deselected”.
  • pytest ~/test/*_test.py --deselect ~/test/test_test.py - this one doesn’t work. The test is being run.
  • pytest . --deselect ~/test/test_test.py - this one doesn’t work either. The test is being run.

Additional info:

I have encountered this with python 3.8.0 and 3.8.1. I have not tried any other version.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 16 (6 by maintainers)

Commits related to this issue

Most upvoted comments

https://github.com/pytest-dev/pytest/blob/d79179a239ac89b318be47c99ff965376d177257/src/_pytest/main.py#L326-L341

That’s the function that takes --deselect-produced tuple and filters the tests. colitem.nodeid is something like (based off the example in OP) test_test.py::test_test, which isn’t the absolute path. Then pybind just says if nodeid.startswith(deselect_tuple), which clearly is false if --deselect contains an absolute path.

Note: nodeid doesn’t depend on current working directory.

However, this logic is fine. What should be changed is how nodeid gets assigned.