I have two simple test setups and I'm trying to group them in one fixture and want the test function to pass in the 'params' to the fixture.
Here's a contrived example, to explain my question. Say I have the following pytest fixture:
@pytest.fixture(scope="module",params=['param1','param2'])def myFixture(request):if request.param =='param1': p =5elif request.param =='param2': p =10return p # would like to set request.param = ['param1'] for myFixturedef test_madeup(myFixture):assert myFixture ==5# would like to set request.param = ['param2'] for myFixturedef test_madeup2(myFixture):assert myFixture ==10
Can I make it so that the params above are passed in as an input to the test_madeup function? So, something like the following:
The above, of course, doesn't work. The real case is a bit more complex, but I just want to know if I can pass the params=['param1','param2'] to the fixture from the test_madeup function.
回答1:
If i understand your question correctly, you basically want to select one instance of a parametrized fixture for executing with a test, by providing some info with the test. It's not possible although we could probably think about a mechanism. I am not sure if the following solution maps to your whole problem, but here is one way to solve the above concrete case:
This runs four tests, because the test_all is executed twice with both fixtures.
If the setup of your fixtures is not heavy, you might also have one fixture that produces a list and an "iterating" parametrized one. A test could then grab the whole list and index it into it.
回答2:
Not sure if this is what you want, but the example case can be implemented like: