Saba
Saba

Reputation: 474

Run a pytest test flow with differest starting data while running each test case

basically if I have test cases written like

class TestClass:
    data = "a"
    def test_1(self):
        TestClass.results = function1(TestClass.data)
    def test_2(self):
        function1(TestClass.results[1])
    def test_3(self):
        function1(TestClass.results[2])
    def test_4(self):
        function1(TestClass.results[3])

and i want to execute all 4 test cases, multiple times with multiple scenarios, in this case by changing "data" variable. how can I do it simply without copy pasting the whole class?

the main goal is to run and log 8 cases in case of 2 flows 12 in case of 3 and etc. (i want to control which test case gets the data and which doesnt)

Upvotes: 1

Views: 118

Answers (1)

Guy
Guy

Reputation: 50809

You can use @pytest.mark.parametrize in class level

@pytest.mark.parametrize('data', ['a', 'b', 'c'])
class TestClass:

    def test_1(self, data):
        function1(data)

    def test_2(self, data):
        function1(data)

    def test_3(self, data):
        function1(data)

    def test_4(self, data):
        function1(data)

Output

example_test.py::TestClass::test_1[a] PASSED                             [  8%]
example_test.py::TestClass::test_1[b] PASSED                             [ 16%]
example_test.py::TestClass::test_1[c] PASSED                             [ 25%]
example_test.py::TestClass::test_2[a] PASSED                             [ 33%]
example_test.py::TestClass::test_2[b] PASSED                             [ 41%]
example_test.py::TestClass::test_2[c] PASSED                             [ 50%]
example_test.py::TestClass::test_3[a] PASSED                             [ 58%]
example_test.py::TestClass::test_3[b] PASSED                             [ 66%]
example_test.py::TestClass::test_3[c] PASSED                             [ 75%]
example_test.py::TestClass::test_4[a] PASSED                             [ 83%]
example_test.py::TestClass::test_4[b] PASSED                             [ 91%]
example_test.py::TestClass::test_4[c] PASSED                             [100%]

Upvotes: 1

Related Questions