问题
For my web server, I have a login
fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function?
from test.fixtures import login
class TestGroups(object):
def test_get_own_only(self, login, login):
pass
回答1:
An alternative is just to copy the fixture function. This is both simple and correctly handles parameterized fixtures, calling the test function with all combinations of parameters for both fixtures. This example code below raises 9 assertions:
import pytest
@pytest.fixture(params=[0, 1, 2])
def first(request):
return request.param
second = first
def test_double_fixture(first, second):
assert False, '{} {}'.format(first, second)
回答2:
I do it with Dummy
class which will implement fixture functionality. Then just call it from your test. Provide clarify method name to better understand what is your test doing.
import pytest
@pytest.fixture
def login():
class Dummy:
def make_user(self):
return 'New user name'
return Dummy()
def test_something(login):
a = login.make_user()
b = login.make_user()
assert a == b
回答3:
The trick is to use mark.parametrize with the "indirect" switch, thus:
@pytest.fixture
def data_repeated(request):
return [deepcopy({'a': 1, 'b': 2}) for _ in range(request.param)]
@pytest.mark.parametrize('data_repeated', [3], indirect=['data_repeated'])
def test(data_repeated):
assert data_repeated == [
{'a': 1, 'b': 2},
{'a': 1, 'b': 2},
{'a': 1, 'b': 2}]
回答4:
I needed my tests to directly call a fixture to overwrite the current instanced result, so I wrote an abstraction layer that contains references to all of my fixtures:
def call_fixture(fixture, session=''):
return {
'fixture_name': fixture_name(session),
}[fixture]
Called with (get_session is another fixture):
call_fixture('fixture_name', get_session)
回答5:
I have done it like so:
limits = [10, 20, 30]
@pytest.fixture(params=limits)
def number(request):
return random.randint(request.param)
@pytest.fixture(params=limits)
def two_numbers(request):
return number(request), number(request)
def test_numbers(two_numbers):
num_1, num_2 = two_numbers
...
来源:https://stackoverflow.com/questions/36100624/pytest-use-same-fixture-twice-in-one-function