[Python] Pytest running very slow for project

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    My pytest setup is running very slow, especially during collection phase.

    So I have put up a pytest setup for my Django project, and each Django app's test-files are located in its own folder, i.e., the tree looks as follows

    root
    |
    +--a
    | |
    | +--tests
    | |
    | +--conftest.py
    | +--testAa.py
    | +--testBa.py
    +--b
    | |
    | +--tests
    | |
    | +--conftest.py
    | +--testAb.py
    | +--testBb.py
    ...
    pytest.ini


    The pytest.ini file specifies where to look for the tests and has the following content

    [pytest]
    DJANGO_SETTINGS_MODULE = project.project.settings.test
    python_files = tests.py test_*.py *_tests.py
    addopts = --reuse-db


    For each app within the tests folder I have a file called contest.py. This file creates a set of objects that are used multiple times in the test files. For instance if an object of class A is used more than once, a contest creates that variable once and the tests use this conftest as input. Each conftest has the decorator @pytest.fixture(scope="function"), and the tests have the decorator @pytest.mark.django_db.

    I don't think the loading times are caused by the conftests, or the decorators discussed in the last paragraph, but rather the tree structure and pytest.ini file I have put up. Are there any rules for what is a good structure for this? As I said, the loading times are extremely high for collecting the tests. To be more accurate, I have about 80 tests, and collecting them takes about 40 seconds. Running them all takes an additional 20.

    Continue reading...

Compartilhe esta Página