Repository navigation
Replies: 1 comment
|
Hi @vallsv You can avoid generating two versions of every test and skipping half of them. Use one resource fixture and indirectly parametrize it with the setup required by each test: import pytest
@pytest.fixture(scope="session")
def resource(request):
with my_resource_setup(request.param) as r:
yield rTests requiring setup 1: @pytest.mark.parametrize(
"resource",
["resource_setup1"],
indirect=True,
scope="session",
)
def test_with_setup1(resource):
...Tests requiring setup 2: @pytest.mark.parametrize(
"resource",
["resource_setup2"],
indirect=True,
scope="session",
)
def test_with_setup2(resource):
...Now each test is collected only once, so you no longer get the artificial skipped cases.
If minimizing expensive setup transitions is critical, also keep tests requiring the same resource setup grouped together; pytest performs resource-based grouping, but you should not rely on arbitrary collection layouts to produce a particular global ordering. I would avoid two independent session fixtures for this because both could remain alive simultaneously. A single parametrized fixture models the mutual-exclusion requirement much more directly. Please mark this answer as accepted if it helped, thank you. |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Found out this question was few time asked, but maybe there is another approach now.
I have a resource which have to be reused as much as possible on the tests, but my tests needs 2 different setup of it.
It sounds like the easiest way to modelize that is to have 2 different fixtures which are mutually exclusive.
I have struggled a lot with package scope, session scope, constraints in different ways, without finding something really nice.
For now i use the following solution, which enforce a sequence for each setup of the resource.
Half of the tests are still marked as skipped, but this ensure to me that the sequence SETUP/TEARDOWN is properly executed to the resource.
Any idea if there is a better approach?
All reactions