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.

Per-use fixture scope

See original GitHub issue

I often find myself needing 2 results from a function-scoped fixture for the same test run.

To do this today, I need a factory:

@pytest.fixture
def foo_factory(a, b, c, d_factory):
    def _foo():
        d = d_factory()
        # Use a, b, c, and d to make foo.
        return foo
    return _foo


@pytest.fixture
def bar(foo_factory):
    foo = foo_factory()
    # Use foo to make some_bar.
    return some_bar


@pytest.fixture
def baz(bar, foo_factory):
    foo = foo_factory()
    # Use foo and bar to make some_baz.
    return some_baz


def test_baz(baz):
    assert baz.whatever

Alternatively, I can invoke foo directly (like a regular function), but then everything foo accepts (i.e. a, b, c, d) must be defined in the calling scope which is unscalable.

If there was, say, a 'call' scope, this could be replaced with:

@pytest.fixture(scope='call')
def foo(a, b, c, d):  # d would also be call-scoped.
    # Use a, b, c, and d to make some_foo.
    return some_foo


# A function-scoped fixture would not be able to depend on a call-scoped fixture.
@pytest.fixture(scope='call')
def bar(foo):
    # Use foo to make some_bar.
    return some_bar


@pytest.fixture(scope='call')
def baz(foo, bar):
    # Use foo and bar to make some_baz.
    return some_baz


def test_baz(baz):
    assert baz.whatever
  • Note that bar and baz get distinct foos.

In other words, this is a feature request for a fixture scope that behaves just like regular function invocations, but you don’t have to do the invocation, pytest does it for you (which keeps the calling scope uncluttered).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
asottilecommented, Apr 21, 2019

Duplicate of #456 I believe, if not feel free to reopen (let’s consolidate discussion on this there)

1reaction
nicoddemuscommented, Apr 4, 2018

Ref: #2703

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest fixtures: explicit, modular, scalable
fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component ...
Read more >
Fixture Scopes - Pytest - Packet Coders
Pytest fixtures have scope. This scope controls how often the fixture is executed or, in other words, how often the setup and teardown...
Read more >
Specifying Fixture Scope - Python Testing with pytest [Book]
Specifying Fixture Scope Fixtures include an optional parameter called scope, which controls how often a fixture gets set up and torn down.
Read more >
Understand 5 Scopes of Pytest Fixtures | by Xiaoxu Gao
Pytest fixtures have five different scopes: function, class, module, package, ... The scope basically controls how often each fixture will be executed.
Read more >
Untitled
PERUSE ALL CONTRACT DOCUMENTS. ... ALL LAY-IN CEILING FIXTURES SHALL BE SECURED TO ... THE SITE TO DETEMINE THE EXACT SCOPE OF ELECTRICAL....
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