PyTest Parametrized Test in order

1.2k views Asked by At

I have 2 pyTest test cases that take a parameter. I want to run them in order with both params, instead of the first test running with all possible values, and then the 2nd test starting.

Consider the below test code:

import pytest


@pytest.mark.parametrize("param1", [("A"), ("B")])
class TestClassTests:
    def test_01_test(self, param1):
        ...

    def test_02_test(self, param1):
        ...

The execution order I am getting is:

  1. test_01_test -- (A)
  2. test_01_test -- (B)
  3. test_02_test -- (A)
  4. test_02_test -- (B)

I want the order to be:

  1. test_01_test -- (A)
  2. test_02_test -- (A)
  3. test_01_test -- (B)
  4. test_02_test -- (B)
1

There are 1 answers

1
Anchit Arnav On

Got answer from here: maintaining order of test execution when parametrizing tests in test class

Just needs a scope="class" in the parametrize decorator.

import pytest


@pytest.mark.parametrize("param1", [("A"), ("B")], scope="class")
class TestClassTests:
    def test_01_test(self, param1):
        ...

    def test_02_test(self, param1):
        ...