Add possibility to pass parameter to the fixture from another fixture

See original GitHub issue

Could you please implement possibility to pass parameter from one fixture to the parent one? I have a complex fixture hierarchy in my test framework. I have some cases when i really need to have this feature.

Example:

@pytest.fixture(scope='session')
def fixture_1(request):
    return request.param

@pytest.fixture
def fixture_2(fixture_1): # Need to pass parameter here to fixture_1
    print (fixture_1)

So far I can pass parameter to the fixture from test using:

@pytest.mark.parametrize('fixture_1', ['parameter'], indirect=True)
def test_1(fixture_1):
     print (fixture_1)

Need to have exactly the same thing from fixture.

Thank you!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nchammascommented, Jul 5, 2018

@sathishaMovvar - You pinged me but I think you meant to ping @nicoddemus instead.

1reaction
Sathisha-Movvar-Bosecommented, Jul 5, 2018

@nchammas thanks for the suggestion, but this still doesn’t solve my problem. the lazy fixture returns an object with only one value for parametrize. I want to use a list of values from the fixture which is used as params in the test.

below code work with lazy fixture but that’s not what I want.


@pytest.fixture(scope="class", params=[2,4,8])
def values(request):
    return request.param

@pytest.mark.parametrize('n_iter', [pytest.lazy_fixture("values")])
class Test_Class1:

instead I want something like below:

@pytest.fixture(scope="class")
def values(request):
    return [2,4,6]

@pytest.mark.parametrize('n_iter', pytest.lazy_fixture("values"))
class Test_Class1:

in which I get error “TypeError: ‘LazyFixture’ object is not iterable”. I found an open question in StackOverflolw similar to my scenario -https://stackoverflow.com/questions/50482416/use-pytest-lazy-fixture-list-values-as-parameters-in-another-fixture

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass a parameterised fixture as a parameter to another ...
2 Answers 2 ; import pytest @pytest.fixture(scope='function') def ; request): first_arg, second_arg = request.param s = Namespace() s.one = ...
Read more >
How to use fixtures — pytest documentation
Once pytest finds them, it runs those fixtures, captures what they returned (if anything), and passes those objects into the test function as...
Read more >
Five Advanced Pytest Fixture Patterns - Inspired Python
Factory Fixture: Fixtures with Arguments. Passing arguments to a fixture is the first thing people want to do with fixtures when they start...
Read more >
Deep dive into Pytest parametrization | by George Shuklin
The fixture called as many times as the number of elements in the iterable of params argument, and the test function is called...
Read more >
Pass Params to Pytest Fixture - Jake Trent
fixture decorator. It is identified by name. The thing that the generator function yields is the fixture value. When the generator is resumed, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found