Skip to content

Commit

Permalink
Replace all usages of "pytest_funcarg__" for @pytest.fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jul 12, 2016
1 parent ad4125d commit 458ecae
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 101 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
2])`` only ran once. Now a failure is raised. Fixes `#460`_. Thanks to
`@nikratio`_ for bug report, `@RedBeardCode`_ and `@tomviner`_ for PR.

*
* ``_pytest.monkeypatch.monkeypatch`` class has been renamed to ``_pytest.monkeypatch.MonkeyPatch``
so it doesn't conflict with the ``monkeypatch`` fixture.

*

Expand Down
4 changes: 2 additions & 2 deletions _pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys

from _pytest.config import hookimpl
from _pytest.monkeypatch import monkeypatch
from _pytest.monkeypatch import MonkeyPatch
from _pytest.assertion import util


Expand Down Expand Up @@ -56,7 +56,7 @@ def pytest_load_initial_conftests(early_config, parser, args):

if mode != "plain":
_load_modules(mode)
m = monkeypatch()
m = MonkeyPatch()
early_config._cleanup.append(m.undo)
m.setattr(py.builtin.builtins, 'AssertionError',
reinterpret.AssertionError) # noqa
Expand Down
8 changes: 5 additions & 3 deletions _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)

defaultfuncargprefixmarker = fixture()
funcarg_prefix_warning = 'declaring fixtures using "pytest_funcarg__" prefix is deprecated ' \
funcarg_prefix_warning = '{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated ' \
'and scheduled to be removed in pytest 4.0.\n' \
'remove the prefix and use the @pytest.fixture decorator instead'

Expand Down Expand Up @@ -1046,16 +1046,18 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
if not callable(obj):
continue
marker = defaultfuncargprefixmarker
self.config.warn('C1', funcarg_prefix_warning.format(name=name))
name = name[len(self._argprefix):]
self.config.warn('C1', funcarg_prefix_warning)
elif not isinstance(marker, FixtureFunctionMarker):
# magic globals with __getattr__ might have got us a wrong
# fixture attribute
continue
else:
if marker.name:
name = marker.name
assert not name.startswith(self._argprefix), name
msg = 'fixtures cannot have "pytest_funcarg__" prefix ' \
'and be decorated with @pytest.fixture:\n%s' % name
assert not name.startswith(self._argprefix), msg
fixturedef = FixtureDef(self, nodeid, name, obj,
marker.scope, marker.params,
unittest=unittest, ids=marker.ids)
Expand Down
11 changes: 7 additions & 4 deletions _pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

from py.builtin import _basestring

import pytest

RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")


def pytest_funcarg__monkeypatch(request):
"""The returned ``monkeypatch`` funcarg provides these
@pytest.fixture
def monkeypatch(request):
"""The returned ``monkeypatch`` fixture provides these
helper methods to modify objects, dictionaries or os.environ::
monkeypatch.setattr(obj, name, value, raising=True)
Expand All @@ -26,7 +29,7 @@ def pytest_funcarg__monkeypatch(request):
parameter determines if a KeyError or AttributeError
will be raised if the set/deletion operation has no target.
"""
mpatch = monkeypatch()
mpatch = MonkeyPatch()
request.addfinalizer(mpatch.undo)
return mpatch

Expand Down Expand Up @@ -93,7 +96,7 @@ def __repr__(self):
notset = Notset()


class monkeypatch:
class MonkeyPatch:
""" Object keeping a record of setattr/item/env/syspath changes. """

def __init__(self):
Expand Down
3 changes: 2 additions & 1 deletion _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ def linecomp(request):
return LineComp()


def pytest_funcarg__LineMatcher(request):
@pytest.fixture(name='LineMatcher')
def LineMatcher_fixture(request):
return LineMatcher


Expand Down
4 changes: 2 additions & 2 deletions _pytest/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
import py
from _pytest.monkeypatch import monkeypatch
from _pytest.monkeypatch import MonkeyPatch


class TempdirFactory:
Expand Down Expand Up @@ -92,7 +92,7 @@ def pytest_configure(config):
available at pytest_configure time, but ideally should be moved entirely
to the tmpdir_factory session fixture.
"""
mp = monkeypatch()
mp = MonkeyPatch()
t = TempdirFactory(config)
config._cleanup.extend([mp.undo, t.finish])
mp.setattr(config, '_tmpdirhandler', t, raising=False)
Expand Down
4 changes: 3 additions & 1 deletion testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ def test_division_zero():
])

class TestFormattedExcinfo:
def pytest_funcarg__importasmod(self, request):

@pytest.fixture
def importasmod(self, request):
def importasmod(source):
source = _pytest._code.Source(source)
tmpdir = request.getfixturevalue("tmpdir")
Expand Down
11 changes: 7 additions & 4 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,21 +795,24 @@ def test_skip_simple(self):

def test_traceback_argsetup(self, testdir):
testdir.makeconftest("""
def pytest_funcarg__hello(request):
import pytest
@pytest.fixture
def hello(request):
raise ValueError("xyz")
""")
p = testdir.makepyfile("def test(hello): pass")
result = testdir.runpytest(p)
assert result.ret != 0
out = result.stdout.str()
assert out.find("xyz") != -1
assert out.find("conftest.py:2: ValueError") != -1
assert "xyz" in out
assert "conftest.py:5: ValueError" in out
numentries = out.count("_ _ _") # separator for traceback entries
assert numentries == 0

result = testdir.runpytest("--fulltrace", p)
out = result.stdout.str()
assert out.find("conftest.py:2: ValueError") != -1
assert "conftest.py:5: ValueError" in out
numentries = out.count("_ _ _ _") # separator for traceback entries
assert numentries > 3

Expand Down
Loading

0 comments on commit 458ecae

Please sign in to comment.