Isaac A
Isaac A

Reputation: 93

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_second and repeat that flow?

Thanks.:)

@pytest.mark.usefixtures('setup')
class TestSomething:

    def run_setup(self):
        pass

    def test_first(self):
        print('test 1')
        name = 'name'
        assert name.isalpha()

    def test_second(self):
        print('test 2')
        name = '12345'
        assert name.isalpha()

Upvotes: 2

Views: 5537

Answers (2)

hoefling
hoefling

Reputation: 66171

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.py
import pytest

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

def pytest_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

Upvotes: 2

Evgeny
Evgeny

Reputation: 4541

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

import pytest

@pytest.mark.order2
def test_foo():
    assert True

@pytest.mark.order1
def test_bar():
    assert True

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.

Upvotes: 1

Related Questions