Merge pull request #11256 from bluetech/scope-pkg-crash
fixtures: fix crash when `parametrize(scope="package")` is used without a Package
This commit is contained in:
commit
c754da10d2
|
@ -0,0 +1 @@
|
||||||
|
Fixed crash on `parametrize(..., scope="package")` without a package present.
|
|
@ -155,6 +155,8 @@ name2pseudofixturedef_key = StashKey[Dict[str, "FixtureDef[Any]"]]()
|
||||||
def add_funcarg_pseudo_fixture_def(
|
def add_funcarg_pseudo_fixture_def(
|
||||||
collector: nodes.Collector, metafunc: "Metafunc", fixturemanager: "FixtureManager"
|
collector: nodes.Collector, metafunc: "Metafunc", fixturemanager: "FixtureManager"
|
||||||
) -> None:
|
) -> None:
|
||||||
|
import _pytest.python
|
||||||
|
|
||||||
# This function will transform all collected calls to functions
|
# This function will transform all collected calls to functions
|
||||||
# if they use direct funcargs (i.e. direct parametrization)
|
# if they use direct funcargs (i.e. direct parametrization)
|
||||||
# because we want later test execution to be able to rely on
|
# because we want later test execution to be able to rely on
|
||||||
|
@ -192,11 +194,17 @@ def add_funcarg_pseudo_fixture_def(
|
||||||
if scope is not Scope.Function:
|
if scope is not Scope.Function:
|
||||||
node = get_scope_node(collector, scope)
|
node = get_scope_node(collector, scope)
|
||||||
if node is None:
|
if node is None:
|
||||||
assert scope is Scope.Class and isinstance(
|
# If used class scope and there is no class, use module-level
|
||||||
collector, _pytest.python.Module
|
# collector (for now).
|
||||||
)
|
if scope is Scope.Class:
|
||||||
# Use module-level collector for class-scope (for now).
|
assert isinstance(collector, _pytest.python.Module)
|
||||||
node = collector
|
node = collector
|
||||||
|
# If used package scope and there is no package, use session
|
||||||
|
# (for now).
|
||||||
|
elif scope is Scope.Package:
|
||||||
|
node = collector.session
|
||||||
|
else:
|
||||||
|
assert False, f"Unhandled missing scope: {scope}"
|
||||||
if node is None:
|
if node is None:
|
||||||
name2pseudofixturedef = None
|
name2pseudofixturedef = None
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1486,6 +1486,23 @@ class TestMetafuncFunctional:
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("scope", ["class", "package"])
|
||||||
|
def test_parametrize_missing_scope_doesnt_crash(
|
||||||
|
self, pytester: Pytester, scope: str
|
||||||
|
) -> None:
|
||||||
|
"""Doesn't crash when parametrize(scope=<scope>) is used without a
|
||||||
|
corresponding <scope> node."""
|
||||||
|
pytester.makepyfile(
|
||||||
|
f"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("x", [0], scope="{scope}")
|
||||||
|
def test_it(x): pass
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
result = pytester.runpytest()
|
||||||
|
assert result.ret == 0
|
||||||
|
|
||||||
|
|
||||||
class TestMetafuncFunctionalAuto:
|
class TestMetafuncFunctionalAuto:
|
||||||
"""Tests related to automatically find out the correct scope for
|
"""Tests related to automatically find out the correct scope for
|
||||||
|
|
Loading…
Reference in New Issue