[Python] Find number of selected tests programatically

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

  1. Stack

    Stack Membro Participativo

    I have a project with 600+ unit tests. These tests use a very time consuming function to log dataframes, which help with debugging. Which is great when I'm debugging 1-2 tests. But when I run all 600 tests this logging takes 30% of total execution time.

    What I want to do is disable this expensive logging whenever the current test run is running say more than 100 tests.

    Here is what I did in my conftest.py:

    def pytest_collection_modifyitems(config, items):
    num_tests = len(items)
    if num_tests > 100:
    print(f'Running {num_tests} tests (more than 100 tests), disabling dataframe logging.')
    os.environ['RUNNING_MORE_THAN_100_TESTS'] = '1'
    else:
    print(f'Running {num_tests} tests.')


    and then in my log_dataframe() method I do nothing if RUNNING_MORE_THAN_100_TESTS is set.

    def log_dataframe(df: DataFrame, logger_obj):
    if os.environ.get('RUNNING_MORE_THAN_100_TESTS'):
    logger_obj.info(f'RUNNING_MORE_THAN_100_TESTS is set. log_dataframe() skipped.')
    return
    else:
    # log df as usual


    But when I run it with some keyword to select specific tests:

    $ pytest -k emailer
    ============================= test session starts ==============================
    platform linux -- Python 3.10.12, pytest-8.3.2, pluggy-1.6.0
    rootdir: /local_disk0/.ephemeral_nfs/envs/pythonEnv-1581bc5c-5c05-4f64-8c4d-2cf4c1be04fb/lib/python3.10/site-packages/my_pkg
    plugins: mock-3.14.0, profiling-1.8.1, cov-5.0.0, anyio-3.5.0
    Running 651 tests (more than 100 tests), disabling dataframe logging.
    collected 651 items / 610 deselected / 41 selected


    So it seems like the hook is called before filters are applied. Is there some other hook that I can use (which is called after items are selected)?

    Continue reading...

Compartilhe esta Página