Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a fixture inside pytest_generate_tests()?

Tags:

pytest

I have a handful of fixtures in conftest.py that work well inside actual test functions. However, I would like to parameterize some tests using pytest_generate_tests() based on the data in some of these fixtures.

What I'd like to do (simplified):

-- conftest.py --
# my fixture returns a list of device names.
@pytest.fixture(scope="module")
def device_list(something):
    return ['dev1', 'dev2', 'dev3', 'test']

-- test001.py --
# generate tests using the device_list fixture I defined above.
def pytest_generate_tests(metafunc):
    metafunc.parametrize('devices', itertools.chain(device_list), ids=repr)

# A test that is parametrized by the above function.
def test_do_stuff(devices):
    assert "dev" in devices

# Output should/would be:
dev1: pass
dev2: pass
dev3: pass
test: FAIL

Of course, the problem I'm hitting is that in pytest_generate_tests(), it complains that device_list is undefined. If I try to pass it in, pytest_generate_tests(metafunc, device_list), I get an error.

E   pluggy.callers.HookCallError: hook call must provide argument 'device_list'

The reason I want to do this is that I use that 'device_list' list inside a bunch of different tests in different files where I want to use pytest_generate_tests() to parametrize test using the same list.

Is this just not possible? What is the point of using pytest_generate_tests() if I have to duplicate my fixtures inside that function?

like image 816
ancker Avatar asked Dec 12 '19 17:12

ancker


People also ask

Can we use fixture in Parametrize pytest?

Being able to reuse fixtures in parametrized tests is a must when we want to avoid repetition. Unfortunately, pytest doesn't support that yet. On the other hand, we can make it happen either by using getfixturevalue in pytest or through a third-party library.

Can a pytest fixture use another fixture?

fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects. fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures.

How does fixture work in pytest?

pytest fixtures are functions attached to the tests which run before the test function is executed. Fixtures are a set of resources that have to be set up before and cleaned up once the Selenium test automation execution is completed.


1 Answers

From what I've gathered over the years, fixtures are pretty tightly coupled to pytest's post-collection stage. I've tried a number of times to do something similar, and it's never really quite worked out.

Instead, you could make a function that does the things your fixture would do, and call that inside the generate_tests hook. Then if you need it still as a fixture, call it again (or save the result or whatever).

like image 130
Colin Dunklau Avatar answered Jan 02 '23 11:01

Colin Dunklau