Running parameterized pytests in a loop

Viewed 650

I am trying to run the pytest in a loop on parameterized data

@pytest.mark.repeat(2)
@pytest.mark.parametrize("username, password",[('abc@abc.com','aaa'),
                        ('def@def.com','ddd'),('efg@efg.com','eee')])

if i run the pytest above, the execution sequence is

( 'abc@abc.com','aaa') executed 2 times , then ('def@def.com','ddd') is executed 2 times and then ('efg@efg.com','eee') is executed.

I am trying to implement the sequence where each of the parameterized values are run for every repeat. With @pytest.mark.repeat(2), i am trying to get the below output:

for 1st repeat: ('abc@abc.com','aaa'),('def@def.com','ddd'),('efg@efg.com','eee')
for 2nd repeat: ('abc@abc.com','aaa'),('def@def.com','ddd'),('efg@efg.com','eee')

Any pointers will be appreciated.

1 Answers

You could put the repeat on a class and then set the --repeat-scope argument to class. See the docs:

If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session, module, class or function (default). It behaves like a scope of the pytest fixture.

function (default) scope repeats each test count or repeat times before executing next test. session scope repeats whole tests session, i.e. all collected tests executed once, then all such tests executed again and etc. class and module behaves similar session , but repeating set of tests is a tests from class or module, not all collected tests.

@pytest.mark.repeat(2)
class Test:
    @pytest.mark.parametrize(
        "username, password",
        [("abc@abc.com", "aaa"), ("def@def.com", "ddd"), ("efg@efg.com", "eee")],
    )
    def test(self, username, password):
        print(username, password)

Then run pytest -s --repeat-scope class test.py:

============================ test session starts =============================
platform darwin -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/flakes/workspace
plugins: requests-mock-1.9.2, cov-2.12.0, mock-3.6.1, repeat-0.9.1
collected 6 items                                                                

test.py abc@abc.com aaa
.def@def.com ddd
.efg@efg.com eee
.abc@abc.com aaa
.def@def.com ddd
.efg@efg.com eee
.

============================= 6 passed in 0.01s ==============================
Related