Reputation: 1
import pytest
@pytest.fixture()
def fixture1(fixture_a):
print('In fixture 1')
<do>
step a
step b
<end>
return <some object1>
@pytest.fixture()
def fixture2(fixture_b):
print('In fixture 2')
<do>
step x
step y
<end>
return <some object2>
def decide():
a = 1
if a == 1:
return fixture1: Object1
else:
return fixture2: Object2
def test_me():
res = decide()
assert res == Object
I have two fixtures arg1 and arg2, now I want to return one of the fixture to the test but this has to be dynamic selection based on a condition. How to do this?
UPDATE: The fixtures arg1 and arg2 have a chain of dependency and they are being used in different tests.
Also, decide function needs to be used in multiple tests.
Upvotes: 0
Views: 1483
Reputation: 3543
I guess that can help - having fixture builder what runs functions(convert fixtures A,B to functions) in case of condition:
def arg1():
print('In arg 1')
return 1
def arg2():
print('In arg 2')
return 2
@pytest.fixture
def env_builder():
if condition:
return arg1()
else
return arg2()
def test_smth(env_builder):
assert 2 == env_builder
your condition is up to you, os.getenv('environment'), or might positional cli argument while running tests
Upvotes: 0
Reputation: 2691
You can pass the arg1
and arg2
to test
and test the condition itself.
def decide():
a = 1
if a == 1:
return 1
else:
return 2
def test_me(fixture1, fixture2):
arg = decide()
if arg == 1:
assert fixture1 == 1
else:
assert fixture2 == 2
Upvotes: 1