1

I want to do something like this

def foo_bar(a, b):
    """ The function I want to test """
    return ...

def subparams(a):
   return [some stuff based on what a is]

@pytest.fixture(scope='module', params=('foo', 'bar'))
def a(request):
    # lengthy setup based on request.param
    yield request.param
    # teardown

@pytest.fixture(params=subparams(a))
def b(request):
    return request.param

def test_foo_bar(a, b):
    result = foo_bar(a, b)
    assert <something about result>

In other words, I have a parameterized fixture a which simply takse a normal list of parameters. For each value of a, I also have a function subparams for generating associated parameters b. For the sake of argument, let's say that subparams is trivially implemented as follows

def subparams(a):
    return [1, 2, 3] if a == 'foo' else [4, 5]

in which case I would want test_foo_bar to be invoked with the following:

  • 'foo', 1
  • 'foo', 2
  • 'foo', 3
  • 'bar', 4
  • 'bar', 5

Unfortunately, the @pytest.fixture(params=subparams(a)) bit doesn't work because at that point a is still just a function, not an instantiation based on a's parameterization. How do I achieve the effect of having test_foo_bar called with those kinds of combinations of a and b where a takes a long time to set up (hence the scope='module' bit) and b is parameterized depending on a?

Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48

0 Answers0