Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate yield tests funcarg prefix #1714

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions 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 Expand Up @@ -185,9 +186,12 @@
Before, you only got exceptions later from ``argparse`` library,
giving no clue about the actual reason for double-added options.

*
* ``yield``-based tests are considered deprecated and will be removed in pytest-4.0.
Thanks `@nicoddemus`_ for the PR.

*
* Using ``pytest_funcarg__`` prefix to declare fixtures is considered deprecated and will be
removed in pytest-4.0 (`#1684`_).
Thanks `@nicoddemus`_ for the PR.

*

Expand Down Expand Up @@ -261,6 +265,7 @@
.. _#1632: https://github.com/pytest-dev/pytest/issues/1632
.. _#1633: https://github.com/pytest-dev/pytest/pull/1633
.. _#1664: https://github.com/pytest-dev/pytest/pull/1664
.. _#1684: https://github.com/pytest-dev/pytest/pull/1684

.. _@DRMacIver: https://github.com/DRMacIver
.. _@RedBeardCode: https://github.com/RedBeardCode
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
9 changes: 8 additions & 1 deletion _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,10 @@ 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 = '{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'



@fixture(scope="session")
Expand Down Expand Up @@ -1042,6 +1046,7 @@ 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):]
elif not isinstance(marker, FixtureFunctionMarker):
# magic globals with __getattr__ might have got us a wrong
Expand All @@ -1050,7 +1055,9 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
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/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ def collect(self):
raise ValueError("%r generated tests with non-unique name %r" %(self, name))
seen[name] = True
l.append(self.Function(name, self, args=args, callobj=call))
msg = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0'
self.config.warn('C1', msg, fslocation=self.fspath)
return l

def getcallargs(self, obj):
Expand All @@ -611,8 +613,6 @@ def hasinit(obj):
return True




class CallSpec2(object):
def __init__(self, metafunc):
self.metafunc = metafunc
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
31 changes: 31 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,34 @@ def test_setup_function(self, testdir):
* setup *test_1*
* call *test_1*
""")


def test_yield_tests_deprecation(testdir):
testdir.makepyfile("""
def func1(arg, arg2):
assert arg == arg2
def test_gen():
yield "m1", func1, 15, 3*5
yield "m2", func1, 42, 6*7
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
'*yield tests are deprecated, and scheduled to be removed in pytest 4.0*',
'*2 passed*',
])


def test_funcarg_prefix_deprecation(testdir):
testdir.makepyfile("""
def pytest_funcarg__value():
return 10

def test_funcarg_prefix(value):
assert value == 10
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
'*declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0*',
'*remove the prefix and use the @pytest.fixture decorator instead*',
'*1 passed*',
])
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
10 changes: 5 additions & 5 deletions testing/code/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,14 @@ def test_compile_and_getsource(self):
#print "block", str(block)
assert str(stmt).strip().startswith('assert')

def test_compilefuncs_and_path_sanity(self):
@pytest.mark.parametrize('name', ['', None, 'my'])
def test_compilefuncs_and_path_sanity(self, name):
def check(comp, name):
co = comp(self.source, name)
if not name:
expected = "codegen %s:%d>" %(mypath, mylineno+2+1)
expected = "codegen %s:%d>" %(mypath, mylineno+2+2)
else:
expected = "codegen %r %s:%d>" % (name, mypath, mylineno+2+1)
expected = "codegen %r %s:%d>" % (name, mypath, mylineno+2+2)
fn = co.co_filename
assert fn.endswith(expected)

Expand All @@ -300,8 +301,7 @@ def check(comp, name):
mypath = mycode.path

for comp in _pytest._code.compile, _pytest._code.Source.compile:
for name in '', None, 'my':
yield check, comp, name
check(comp, name)

def test_offsetless_synerr(self):
pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode='eval')
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