Per-use fixture scope
See original GitHub issueI 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
andbaz
get distinctfoo
s.
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:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Duplicate of #456 I believe, if not feel free to reopen (let’s consolidate discussion on this there)
Ref: #2703