Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested parametrized tests (pytest)

Tags:

python

pytest

I'm trying to nest parametrized tests. The code below does it, but I only want to execute the code on param1 when it changes ("print param1" is time-consuming)

@pytest.mark.parametrize("param3", ["p31", "p32"])
@pytest.mark.parametrize("param2", ["p21", "p22"])
@pytest.mark.parametrize("param1", ["p11", "p12"])
def test_one(param1, param2, param3):
    print param1  # goal is to run this only when param1 changes
    print param2, param3

I tried this, but it does not seem to work:

@pytest.mark.parametrize("param1", ["p11", "p12"])
def test_one(param1, param2, param3):
   print param1  # goal is to run this only when param1 changes
   @pytest.mark.parametrize("param3", ["p31", "p32"])
   @pytest.mark.parametrize("param2", ["p21", "p22"])
   def test_two(param2, param3):
      print param2, param3

Does anybody have an idea?

like image 836
egabro Avatar asked Oct 20 '16 21:10

egabro


1 Answers

A colleague gave me a solution:

@pytest.fixture(scope="class", params=["B1","B2"])
def two(request):
   print "\n SETUP", request.param
   yield request.param
   #print "\n UNDO", request.param

@pytest.fixture(scope="class", params=["A1", "A2"])
def one(request):
   print "\n SETUP", request.param
   yield request.param
   #print "\n UNDO", request.param

class Test_myclass():
   @pytest.mark.parametrize("param4", ["D1", "D2"])
   @pytest.mark.parametrize("param3", ["C1", "C2"])
   def test_three(self, one, two, param3, param4):
      print "\n ({0} {1}) RUN ".format(one, two), param3, param4,
like image 96
egabro Avatar answered Oct 06 '22 01:10

egabro