Merge pull request #4209 from nicoddemus/fixture-named-request

Issue a warning when a fixture named 'request' is collected
This commit is contained in:
Ankit Goel 2018-10-21 05:49:08 +05:30 committed by GitHub
commit f30911d3af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 2 deletions

2
changelog/611.bugfix.rst Normal file
View File

@ -0,0 +1,2 @@
Naming a fixture ``request`` will now raise a warning: the ``request`` fixture is internal and
should not be overwritten as it will lead to internal errors.

View File

@ -11,7 +11,11 @@ in case of warnings which need to format their messages.
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
from _pytest.warning_types import UnformattedWarning, RemovedInPytest4Warning from _pytest.warning_types import (
UnformattedWarning,
RemovedInPytest4Warning,
PytestDeprecationWarning,
)
MAIN_STR_ARGS = RemovedInPytest4Warning( MAIN_STR_ARGS = RemovedInPytest4Warning(
@ -55,6 +59,10 @@ FIXTURE_FUNCTION_CALL = UnformattedWarning(
"See https://docs.pytest.org/en/latest/fixture.html for more information.", "See https://docs.pytest.org/en/latest/fixture.html for more information.",
) )
FIXTURE_NAMED_REQUEST = PytestDeprecationWarning(
"'request' is a reserved name for fixtures and will raise an error in future versions"
)
CFG_PYTEST_SECTION = UnformattedWarning( CFG_PYTEST_SECTION = UnformattedWarning(
RemovedInPytest4Warning, RemovedInPytest4Warning,
"[pytest] section in {filename} files is deprecated, use [tool:pytest] instead.", "[pytest] section in {filename} files is deprecated, use [tool:pytest] instead.",

View File

@ -32,7 +32,7 @@ from _pytest.compat import (
get_real_method, get_real_method,
_PytestWrapper, _PytestWrapper,
) )
from _pytest.deprecated import FIXTURE_FUNCTION_CALL from _pytest.deprecated import FIXTURE_FUNCTION_CALL, FIXTURE_NAMED_REQUEST
from _pytest.outcomes import fail, TEST_OUTCOME from _pytest.outcomes import fail, TEST_OUTCOME
FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}' FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}'
@ -1029,6 +1029,9 @@ class FixtureFunctionMarker(object):
function = wrap_function_to_warning_if_called_directly(function, self) function = wrap_function_to_warning_if_called_directly(function, self)
name = self.name or function.__name__
if name == "request":
warnings.warn(FIXTURE_NAMED_REQUEST)
function._pytestfixturefunction = self function._pytestfixturefunction = self
return function return function

View File

@ -4,6 +4,8 @@ import os
import pytest import pytest
pytestmark = pytest.mark.pytester_example_path("deprecated")
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_yield_tests_deprecation(testdir): def test_yield_tests_deprecation(testdir):
@ -392,3 +394,13 @@ def test_pycollector_makeitem_is_deprecated():
with pytest.warns(RemovedInPytest4Warning): with pytest.warns(RemovedInPytest4Warning):
collector.makeitem("foo", "bar") collector.makeitem("foo", "bar")
assert collector.called assert collector.called
def test_fixture_named_request(testdir):
testdir.copy_example()
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*'request' is a reserved name for fixtures and will raise an error in future versions"
]
)

View File

@ -0,0 +1,10 @@
import pytest
@pytest.fixture
def request():
pass
def test():
pass