Pytests doesn't find a test if explicitely named

Question:

Have the following project tree (just a tutorial to learn pytest):

(pytest) bob@Roberts-Mac-mini ds % tree
.
├── ds
│   └── __init__.py
└── tests
    ├── __pycache__
    │   ├── test_compare.cpython-311-pytest-7.2.0.pyc
    │   ├── test_square.cpython-311-pytest-7.2.0.pyc
    │   └── test_stack.cpython-311-pytest-7.2.0.pyc
    ├── test_compare.py
    └── test_square.py

4 directories, 6 files

and from its root all works as expected if I just run pytest:

===================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/bob/Documents/work/pytest_tut
collected 6 items                                                                                                                                                                             

ds/tests/test_compare.py F..                                                                                                                                                            [ 50%]
ds/tests/test_square.py ..F                                                                                                                                                             [100%]

========================================================================================== FAILURES ===========================================================================================
________________________________________________________________________________________ test_greater _________________________________________________________________________________________

    def test_greater():
       num = 100
>      assert num > 100
E      assert 100 > 100

ds/tests/test_compare.py:3: AssertionError
________________________________________________________________________________________ test_equality ________________________________________________________________________________________

    def test_equality():
>      assert 10 == 11
E      assert 10 == 11

ds/tests/test_square.py:12: AssertionError
=================================================================================== short test summary info ===================================================================================
FAILED ds/tests/test_compare.py::test_greater - assert 100 > 100
FAILED ds/tests/test_square.py::test_equality - assert 10 == 11
================================================================================= 2 failed, 4 passed in 0.03s =================================================================================

but if I run:

(pytest) bob@Roberts-Mac-mini pytest_tut % pytest test_square.py
===================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/bob/Documents/work/pytest_tut
collected 0 items                                                                                                                                                                             

==================================================================================== no tests ran in 0.00s ====================================================================================
ERROR: file or directory not found: test_square.py

the test_square.py module is not found, unlike when pytest was invoked without arguments.

What am I doing wrong? Thanks

Asked By: Robert Alexander

||

Answers:

You need to specify the full path when running a single test with pytest, like this:

pytest tests/test_square.py

Running pytest without args searches in all the tests sub-directories while if you indicate a specific test you need to qualify its full path.

Answered By: Caridorc
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.