Configure pytest using testpaths to search for tests inside tests folder
Asked Answered
O

1

6

I'm trying to improve the test collection speed by including the testpaths option in the pytest.ini file.

Inside each App folder I have a tests file where I put my tests, so I'm including it as follows:

[pytest]
testpaths = tests

However, no tests are collected. It returns the error: ERROR: file or directory not found: tests

What I'm I missing?

Oui answered 23/11, 2021 at 13:49 Comment(3)
According to the documentation, testpaths is a list of directories that should be searched for tests. I don't think this is what you are providing here.Damick
testpaths should be relative to the project root, so if you have a subdirectory app1/tests, then testpaths = tests won't work. Do a configuration per-app and place one pytest.ini with testpaths = tests in each of your app directories.Anselma
In my project pytest successfully find tests when a path of the app1/tests is not specified. If I set testpaths = tests, pytest catch all tests in ./*/tests/test_*.py subdirectories.Chenoweth
L
2

For example, you should put pytest.ini in the root directly of django-project as shown below:

django-project
 |-core
 |  └-settings.py
 |-my_app1
 |-my_app2
 |-pytest.ini # Here
 └-tests
    |-__init__.py
    └-test_1.py

In addition, if the specified testpaths below doesn't exist, the tests in tests folder are run:

# "pytest.ini"

[pytest]
DJANGO_SETTINGS_MODULE = core.settings
testpaths = my_tests # Doesn't exist
Lamere answered 2/9, 2023 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.