Setting
Suppose I have conftest.py where I defined resources for tests
class ClassifyRequest:
pass
class OCRRequest:
pass
@pytest.fixture(params=[ClassifyRequest, OCRRequest])
def api_request(request):
return request.param() # return one of the request instances
Then, in test_api.py I use the parametrized fixture to test a service:
def test_service(api_request):
response = send_request(api_request)
assert response.ok()
All good, but then I want to test specialized fixture api_request[ClassifyRequest]:
@pytest.mark.usefixture("api_request[ClassifyRequest]")
def test_classification():
# do something with the api_request fixture
Question
What is the way to specialize a parametrized fixture for a test function? I have two ideas:
- Just use a non-parametrized fixture. This inevitably leads to boilerplate code.
- Remove explicit parametrization from the fixture decorator and use indirect parametrization like
Replacing class parameters with strings equals creating two sources of configuration. What if I want to add another parameter? Do I have to design a new string for it, as well as instantiate an object inside@pytest.mark.parametrize("response_class", ["classify", "ocr"]) def test_api(api_request): # do smthapi_requestfixture indirectly? - The same, except keep class parametrization and move classes outside
conftest.pyas pytest can't import names from that module with import statements.
Additional info
pytest==6.0.1