Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest - How to make it search recursively?

I have the following tests directory tree:

tests/
  subfolder_1/
    test_1.py
  subfolder_2/
    subsubfolder/
      subsubsubfolder/
        test_2.py

py.test only finds test_1.py. How can I make it find test_2.py?

like image 339
W2a Avatar asked Feb 04 '23 01:02

W2a


2 Answers

pytest always searches recursively unless you tell it otherwise (with the norecursedirs option).

If pytest is failing to collect some of your tests, double check that:

  1. the test files are somewhere under the testpaths configuration pattern (default: the current directory),
  2. the test files match the python_files configuration pattern (default: test_*.py and *_test.py),
  3. the test classes match the python_classes configuration pattern (default: Test*), and
  4. the test functions match the python_functions configuration pattern (default: test*).
like image 160
Ian Lesperance Avatar answered Feb 06 '23 16:02

Ian Lesperance


Pytest has some issues finding files named test.py or similar. Try giving it a more specialised name such as test_subfolder1.py and test_subfolder2.py or so. Also make sure that all you folders are packages i.e. have __init__.py file in them.

like image 36
hspandher Avatar answered Feb 06 '23 16:02

hspandher