The problem:
Let us say that there are 10 steps required for a process to complete. I start the steps of the process 1 by 1 and in between these steps, I would like to make different assertions. After each assertion, I proceed with the rest of the steps.
I want each of these assertion to happen as a test case. So if there are 5 assertions, I need 5 testcases. If there are 2 set of parameterized values, then I need to do 5 assertions each, so 10 testcases will be executed. I can put all the steps in a single test case itself, but I specifically want the assertions to be separate testcases.
The following is my code.
# conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--data", action="store", default="1,2 3,4 5,6", help="Data for parametrization")
def pytest_generate_tests(metafunc):
data_str = metafunc.config.getoption("data")
# data_list = [(1, 2), (3, 4), (5, 6)]
data_list = [tuple(map(int, item.split(','))) for item in data_str.split()]
metafunc.parametrize("arithmetic_data", data_list, indirect=True)
@pytest.fixture(scope="function")
def arithmetic_data(request):
return request.param
# test_allure.py
import pytest
class TestClass:
@pytest.fixture
def arithmetic_data(self, arithmetic_data):
self.a, self.b = arithmetic_data # Step 1
self.a, self.b = self.a * 2, self.b * 3
self.c, self.d = self.a * 10, self.b * 10 # Step 4
def test_addition(self, arithmetic_data): # Step 2
result = self.a + self.b
assert result == self.a + self.b, f"Addition failed: {self.a} + {self.b} != {result}"
def test_subtraction(self, arithmetic_data): # Step 3
result = self.a - self.b
assert result == self.a - self.b, f"Subtraction failed: {self.a} - {self.b} != {result}"
def test_multiplication(self, arithmetic_data): # Step 5
result = self.c * self.d
assert result == self.c * self.d, f"Multiplication failed: {self.c} * {self.d} != {result}"
def test_square(self, arithmetic_data): # Step 6
result = self.a * self.a
assert result == self.a * self.a, f"Square failed: {self.a}^2 != {result}"
What I think I did:
I got the parameterized value into a class, did some post processing in arithmetic_data
function (Step 1). I passed this function as parameter to the first test case (test_addition
– Step 2) and executed it followed by second test case (test_subtraction
– Step 3).
At this place, I want to execute Step 4, however I had executed it alongside Step 1 itself. I then pass these values to test_multiplication
(Step 5). I then execute the next step which is the last test case.
I know this is bad, because I can’t run this in parallel because the steps have to be executed sequentially. But I would ideally want each process to be run in parallel, for each parameterized set of values. Any pointers on resolving this will be very helpful. Thanks.