Skip to content Skip to sidebar Skip to footer

Pytest - Test Flow Order

I have a pytest code similar to below... if i run it with --count 3 it will run the test_first 3 times then test_second 3 times. What if i would like it to run test_first, test_sec

Solution 1:

You can implement it yourself. Take a look at the pytest_collection_modifyitems hook where you can alter the list of tests to be executed. Example:

# conftest.pyimport pytest

defpytest_addoption(parser):
    parser.addoption('--numrepeats', action='store', type=int, default=1)

defpytest_collection_modifyitems(items):
    numrepeats = pytest.config.getoption('--numrepeats')
    items.extend(items * (numrepeats - 1))

When put into a conftest.py file in the tests root dir, this code adds a new command line option numrepeats that will repeat the test run n times:

$ pytest --numrepeats 3

Solution 2:

Based on https://pytest-ordering.readthedocs.io (alpha) plug-in you can do:

import pytest

@pytest.mark.order2deftest_foo():
    assertTrue@pytest.mark.order1deftest_bar():
    assertTrue

See also a discussion at Test case execution order in pytest.

My personal take on this if your tests require sequence, they are not really well isolated and some other test suit design is possible.

Post a Comment for "Pytest - Test Flow Order"