question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pytest-lazy-fixture doesn't work for deep nesting

See original GitHub issue

pytest-lazy-fixture doesn’t work for deep nesting

platform linux – Python 3.5.2, pytest-3.10.1, py-1.7.0, pluggy-0.8.1 plugins: timeout-1.3.0, celery-4.2.1, repeat-0.7.0, reportportal-1.0.4, assume-1.2, lazy-fixture-0.5.1

Example:

import pytest

@pytest.fixture(scope='function')
def fixt1():
    return 111

@pytest.fixture(scope='function')
def fixt2():
    return pytest.lazy_fixture("fixt1")

@pytest.fixture(scope='function')
def fixt3():
    return pytest.lazy_fixture("fixt2")

def test(fixt3):
    print("Value in test: {}".format(fixt3))

Returns:

Value in test: <LazyFixture "fixt1">

Should be:

Value in test: 111

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
smariecommented, Jun 12, 2020

This is a relatively old post, but pytest-cases might be a solution for you. Its parametrize_plus decorator supports referencing fixtures that are themselves parametrized, with fixture_ref.

In short pytest-cases provides a generalization of the great pytest-lazy-fixture to support more complex use-cases such as parametrization of lazy fixtures (and many other things such as fixture unions, etc.)

1reaction
YannickJadoulcommented, Feb 12, 2019

@bigbZik The main problem to get such a thing working on the long term is that pytest has two logical phases: first, all tests (with different parametrizations) are collected; and only then all tests (and necessary fixtures) are executed.

So in your example, only when fixt3 gets executed, pytest.lazy_fixture("fixt2") gets returned. So if fixt2 would be parametrized, there’d be no way for test to be instantiated multiple times during test collection.

But in this case, what’s the problem with these two alternatives?

import pytest

@pytest.fixture(scope='function')
def fixt1():
    return 111

@pytest.fixture(scope='function')
def fixt2(fixt1):
    return fixt1

@pytest.fixture(scope='function')
def fixt3(fixt2):
    return fixt2

def test(fixt3):
    print("Value in test: {}".format(fixt3))

or

import pytest

@pytest.fixture(scope='function')
def fixt1():
    return 111

@pytest.fixture(scope='function', params=[pytest.lazy_fixture('fixt1')])
def fixt2(request):
    return request.param

@pytest.fixture(scope='function', params=[pytest.lazy_fixture('fixt2')])
def fixt3(request):
    return request.param

def test(fixt3):
    print("Value in test: {}".format(fixt3))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pytest parametrized fixture with pytest-lazy-fixture fails if ...
Clearly debugging the plugin manager of pytest is unfeasible but I noticed the pytest_configure function inside pytest-lazy-fixture never gets ...
Read more >
Python pytest - The Blue Book
Pytest. pytest is a Python framework to makes it easy to write small tests, yet scales to support complex functional testing for applications...
Read more >
4. Writing Great Code - The Hitchhiker's Guide to Python [Book]
This chapter focuses on best practices for writing great Python code. ... available for importing deeply nested packages: import very.deep.module as mod ....
Read more >
Changelog — Schemathesis 3.17.5 documentation
Internal error when Schemathesis doesn't have permission to create its ... Ignored Open API specific keywords & types in schemas with deeply nested...
Read more >
Best Practices - Apache Airflow
The DAG that has simple linear structure A -> B -> C will experience less delays in task scheduling than DAG that has...
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