Implemented the dynamic scope feature.
This commit is contained in:
committed by
Andrzej Klajnert
parent
404cf0c872
commit
10bf6aac76
@@ -2216,6 +2216,68 @@ class TestFixtureMarker:
|
||||
["*ScopeMismatch*You tried*function*session*request*"]
|
||||
)
|
||||
|
||||
def test_dynamic_scope(self, testdir):
|
||||
testdir.makeconftest(
|
||||
"""
|
||||
import pytest
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--extend-scope", action="store_true", default=False)
|
||||
|
||||
|
||||
def dynamic_scope(fixture_name, config):
|
||||
if config.getoption("--extend-scope"):
|
||||
return "session"
|
||||
return "function"
|
||||
|
||||
|
||||
@pytest.fixture(scope=dynamic_scope)
|
||||
def dynamic_fixture(calls=[]):
|
||||
calls.append("call")
|
||||
return len(calls)
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
testdir.makepyfile(
|
||||
"""
|
||||
def test_first(dynamic_fixture):
|
||||
assert dynamic_fixture == 1
|
||||
|
||||
|
||||
def test_second(dynamic_fixture):
|
||||
assert dynamic_fixture == 2
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
reprec = testdir.inline_run()
|
||||
reprec.assertoutcome(passed=2)
|
||||
|
||||
reprec = testdir.inline_run("--extend-scope")
|
||||
reprec.assertoutcome(passed=1, failed=1)
|
||||
|
||||
def test_dynamic_scope_bad_return(self, testdir):
|
||||
testdir.makepyfile(
|
||||
"""
|
||||
import pytest
|
||||
|
||||
def dynamic_scope(**_):
|
||||
return "wrong-scope"
|
||||
|
||||
@pytest.fixture(scope=dynamic_scope)
|
||||
def fixture():
|
||||
pass
|
||||
|
||||
"""
|
||||
)
|
||||
result = testdir.runpytest()
|
||||
result.stdout.fnmatch_lines(
|
||||
"Fixture 'fixture' from test_dynamic_scope_bad_return.py "
|
||||
"got an unexpected scope value 'wrong-scope'"
|
||||
)
|
||||
|
||||
def test_register_only_with_mark(self, testdir):
|
||||
testdir.makeconftest(
|
||||
"""
|
||||
@@ -4009,3 +4071,54 @@ def test_fixture_named_request(testdir):
|
||||
" *test_fixture_named_request.py:5",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def test_fixture_duplicated_arguments(testdir):
|
||||
testdir.makepyfile(
|
||||
"""
|
||||
import pytest
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
|
||||
@pytest.fixture("session", scope="session")
|
||||
def arg(arg):
|
||||
pass
|
||||
|
||||
def test_error():
|
||||
assert (
|
||||
str(excinfo.value)
|
||||
== "The fixture arguments are defined as positional and keyword: scope. "
|
||||
"Use only keyword arguments."
|
||||
)
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
reprec = testdir.inline_run()
|
||||
reprec.assertoutcome(passed=1)
|
||||
|
||||
|
||||
def test_fixture_with_positionals(testdir):
|
||||
"""Raise warning, but the positionals should still works."""
|
||||
testdir.makepyfile(
|
||||
"""
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS
|
||||
|
||||
with pytest.warns(pytest.PytestDeprecationWarning) as warnings:
|
||||
@pytest.fixture("function", [0], True)
|
||||
def arg(monkeypatch):
|
||||
monkeypatch.setenv("AUTOUSE_WORKS", "1")
|
||||
|
||||
|
||||
def test_autouse():
|
||||
assert os.environ.get("AUTOUSE_WORKS") == "1"
|
||||
assert str(warnings[0].message) == str(FIXTURE_POSITIONAL_ARGUMENTS)
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
reprec = testdir.inline_run()
|
||||
reprec.assertoutcome(passed=1)
|
||||
|
||||
Reference in New Issue
Block a user