Battery test based on a given logic and parameterize them by fixtures #8623
Unanswered
jaraqueffdc
asked this question in
Q&A
Replies: 1 comment
|
Hi @jaraqueffdc A parametrized fixture is a good fit for this. Put the implementations/specifications in the fixture parameters: @pytest.fixture(
params=[SpecA, SpecB, SpecC],
ids=lambda spec: spec.__name__,
)
def spec(request):
return request.param()Then write the common battery only once: def test_feature_one(spec):
assert spec.feature_one()
def test_feature_two(spec):
assert spec.feature_two()pytest will run each test once for every fixture parameter. If an implementation needs additional mocks/setup, another fixture can depend on @pytest.fixture
def prepared_spec(spec, monkeypatch):
if isinstance(spec, SpecB):
# SpecB-specific setup
...
return specI would generally keep that dependency explicit rather than making it autouse unless every test in its scope should automatically become parametrized by every specification. Please mark this answer as accepted if it helped, thank you. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi!
I have worked with pytest in the past and I loved it but for one reason or another all the project I have worked in use unittest but I found an use case where I cannot use unittest and I was hopping pytest would allow me to use it.
I am sorry if this is simple to do but I think the issue is that I do not know what to look for exactly 😅
The scenario is the following:
I have all the tests defined and I would like to be able to use those but being able to configure each set of tests because we have 3 different specifications. All tests depends on a fixture
dswhich is the specification itself but for some of them we will also need mocking which I was planning to do with a yielding fixture with autouse.However, I am not sure how to separate the three test sets so each fixture is used with the written tests as well as allowing the mocking fixtures to only apply when running for that given specification.
I am not sure it is clear but please let me know if you have any question about the intended use or any suggestion.
Thanks a lot!
All reactions