Skip to content

Commit

Permalink
Merge pull request #4209 from nicoddemus/fixture-named-request
Browse files Browse the repository at this point in the history
Issue a warning when a fixture named 'request' is collected
  • Loading branch information
crazymerlyn authored Oct 21, 2018
2 parents b432f12 + 7bb51b8 commit f30911d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog/611.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
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(
Expand Down Expand Up @@ -55,6 +59,10 @@
"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(
RemovedInPytest4Warning,
"[pytest] section in {filename} files is deprecated, use [tool:pytest] instead.",
Expand Down
5 changes: 4 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
get_real_method,
_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

FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}'
Expand Down Expand Up @@ -1029,6 +1029,9 @@ def __call__(self, function):

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
return function

Expand Down
12 changes: 12 additions & 0 deletions testing/deprecated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

pytestmark = pytest.mark.pytester_example_path("deprecated")


@pytest.mark.filterwarnings("default")
def test_yield_tests_deprecation(testdir):
Expand Down Expand Up @@ -392,3 +394,13 @@ def _makeitem(self, *k):
with pytest.warns(RemovedInPytest4Warning):
collector.makeitem("foo", "bar")
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"
]
)
10 changes: 10 additions & 0 deletions testing/example_scripts/deprecated/test_fixture_named_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


@pytest.fixture
def request():
pass


def test():
pass

0 comments on commit f30911d

Please sign in to comment.