How to parameterize both test case AND setup in UnitTest

Viewed 45

everyone. I've got 2 fixtures and 2 test cases. Here's what I've come up with to glue them together (to avoid writing 4 separate test cases):

from unittest import TestCase


class TypeAFixture:
    def setUp(self):
        print("\nSetting up type A")


class TypeBFixture:
    def setUp(self):
        print("\nSetting up type B")


class Scenario:
    def test_case_1(self):
        print("testing Case 1")

    def test_case_2(self):
        print("testing Case 2")


class TestA(TypeAFixture, Scenario, TestCase):
    pass


class TestB(TypeBFixture, Scenario, TestCase):
    pass

works as expected, with the following output (I'm running it using pytest):

main.py::TestA::test_case_1 
Setting up type A
testing Case 1
PASSED
main.py::TestA::test_case_2 
Setting up type A
testing Case 2
PASSED
main.py::TestB::test_case_1 
Setting up type B
testing Case 1
PASSED
main.py::TestB::test_case_2 
Setting up type B
testing Case 2
PASSED

The problem is - I need to expand on this and implement an additional parametrization to both setUps as well. I've tried different approaches (using subTest, providing additional arguments to fixtures, etc), but nothing seems to work so far. I'm expecting the following output:

main.py::TestA::test_case_1 
Setting up type A
Setting up param X
testing Case 1
PASSED
main.py::TestA::test_case_2 
Setting up type A
Setting up param Y
testing Case 2
PASSED
main.py::TestB::test_case_1 
Setting up type B
Setting up param X
testing Case 1
PASSED
main.py::TestB::test_case_2 
Setting up type B
Setting up param Y
testing Case 2
PASSED

what would be the correct way to ipmlement this? Any advice would be highly appreciated.

0 Answers
Related