Merge pull request #1626 from tomviner/issue1625/rename-getfuncargvalue

issue1625, rename getfuncargvalue to getfixturevalue
This commit is contained in:
Bruno Oliveira 2016-06-24 08:58:32 -03:00 committed by GitHub
commit 3d263c64c3
9 changed files with 56 additions and 36 deletions

View File

@ -27,6 +27,10 @@
Thanks `@Vogtinator`_ for reporting. Thanks to `@RedBeardCode`_ and Thanks `@Vogtinator`_ for reporting. Thanks to `@RedBeardCode`_ and
`@tomviner`_ for PR. `@tomviner`_ for PR.
* Rename ``getfuncargvalue`` to ``getfixturevalue``. ``getfuncargvalue`` is
deprecated but still present. Thanks to `@RedBeardCode`_ and `@tomviner`_
for PR (`#1626`_).
* *
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580 .. _#1580: https://github.com/pytest-dev/pytest/pull/1580
@ -34,6 +38,7 @@
.. _#1597: https://github.com/pytest-dev/pytest/pull/1597 .. _#1597: https://github.com/pytest-dev/pytest/pull/1597
.. _#460: https://github.com/pytest-dev/pytest/pull/460 .. _#460: https://github.com/pytest-dev/pytest/pull/460
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553 .. _#1553: https://github.com/pytest-dev/pytest/issues/1553
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626
.. _@graingert: https://github.com/graingert .. _@graingert: https://github.com/graingert
.. _@taschini: https://github.com/taschini .. _@taschini: https://github.com/taschini

View File

@ -70,7 +70,7 @@ class DoctestItem(pytest.Item):
def setup(self): def setup(self):
if self.dtest is not None: if self.dtest is not None:
self.fixture_request = _setup_fixtures(self) self.fixture_request = _setup_fixtures(self)
globs = dict(getfixture=self.fixture_request.getfuncargvalue) globs = dict(getfixture=self.fixture_request.getfixturevalue)
self.dtest.globs.update(globs) self.dtest.globs.update(globs)
def runtest(self): def runtest(self):

View File

@ -5,6 +5,7 @@ import inspect
import re import re
import types import types
import sys import sys
import warnings
import py import py
import pytest import pytest
@ -1469,7 +1470,7 @@ class FixtureRequest(FuncargnamesCompatAttr):
fixturedefs = self._arg2fixturedefs.get(argname, None) fixturedefs = self._arg2fixturedefs.get(argname, None)
if fixturedefs is None: if fixturedefs is None:
# we arrive here because of a a dynamic call to # we arrive here because of a a dynamic call to
# getfuncargvalue(argname) usage which was naturally # getfixturevalue(argname) usage which was naturally
# not known at parsing/collection time # not known at parsing/collection time
fixturedefs = self._fixturemanager.getfixturedefs( fixturedefs = self._fixturemanager.getfixturedefs(
argname, self._pyfuncitem.parent.nodeid) argname, self._pyfuncitem.parent.nodeid)
@ -1564,7 +1565,7 @@ class FixtureRequest(FuncargnamesCompatAttr):
fixturenames = getattr(item, "fixturenames", self.fixturenames) fixturenames = getattr(item, "fixturenames", self.fixturenames)
for argname in fixturenames: for argname in fixturenames:
if argname not in item.funcargs: if argname not in item.funcargs:
item.funcargs[argname] = self.getfuncargvalue(argname) item.funcargs[argname] = self.getfixturevalue(argname)
def cached_setup(self, setup, teardown=None, scope="module", extrakey=None): def cached_setup(self, setup, teardown=None, scope="module", extrakey=None):
""" (deprecated) Return a testing resource managed by ``setup`` & """ (deprecated) Return a testing resource managed by ``setup`` &
@ -1598,17 +1599,23 @@ class FixtureRequest(FuncargnamesCompatAttr):
self._addfinalizer(finalizer, scope=scope) self._addfinalizer(finalizer, scope=scope)
return val return val
def getfuncargvalue(self, argname): def getfixturevalue(self, argname):
""" Dynamically retrieve a named fixture function argument. """ Dynamically run a named fixture function.
As of pytest-2.3, it is easier and usually better to access other Declaring fixtures via function argument is recommended where possible.
fixture values by stating it as an input argument in the fixture But if you can only decide whether to use another fixture at test
function. If you only can decide about using another fixture at test
setup time, you may use this function to retrieve it inside a fixture setup time, you may use this function to retrieve it inside a fixture
function body. or test function body.
""" """
return self._get_active_fixturedef(argname).cached_result[0] return self._get_active_fixturedef(argname).cached_result[0]
def getfuncargvalue(self, argname):
""" Deprecated, use getfixturevalue. """
warnings.warn(
"use of getfuncargvalue is deprecated, use getfixturevalue",
DeprecationWarning)
return self.getfixturevalue(argname)
def _get_active_fixturedef(self, argname): def _get_active_fixturedef(self, argname):
try: try:
return self._fixturedefs[argname] return self._fixturedefs[argname]
@ -1624,7 +1631,7 @@ class FixtureRequest(FuncargnamesCompatAttr):
raise raise
# remove indent to prevent the python3 exception # remove indent to prevent the python3 exception
# from leaking into the call # from leaking into the call
result = self._getfuncargvalue(fixturedef) result = self._getfixturevalue(fixturedef)
self._funcargs[argname] = result self._funcargs[argname] = result
self._fixturedefs[argname] = fixturedef self._fixturedefs[argname] = fixturedef
return fixturedef return fixturedef
@ -1640,7 +1647,7 @@ class FixtureRequest(FuncargnamesCompatAttr):
l.append(fixturedef) l.append(fixturedef)
current = current._parent_request current = current._parent_request
def _getfuncargvalue(self, fixturedef): def _getfixturevalue(self, fixturedef):
# prepare a subrequest object before calling fixture function # prepare a subrequest object before calling fixture function
# (latter managed by fixturedef) # (latter managed by fixturedef)
argname = fixturedef.argname argname = fixturedef.argname

View File

@ -32,7 +32,7 @@ class Writer:
def pytest_funcarg__a(request): def pytest_funcarg__a(request):
with Writer("request") as writer: with Writer("request") as writer:
writer.docmethod(request.getfuncargvalue) writer.docmethod(request.getfixturevalue)
writer.docmethod(request.cached_setup) writer.docmethod(request.cached_setup)
writer.docmethod(request.addfinalizer) writer.docmethod(request.addfinalizer)
writer.docmethod(request.applymarker) writer.docmethod(request.applymarker)

View File

@ -327,7 +327,7 @@ class TestFormattedExcinfo:
def pytest_funcarg__importasmod(self, request): def pytest_funcarg__importasmod(self, request):
def importasmod(source): def importasmod(source):
source = _pytest._code.Source(source) source = _pytest._code.Source(source)
tmpdir = request.getfuncargvalue("tmpdir") tmpdir = request.getfixturevalue("tmpdir")
modpath = tmpdir.join("mod.py") modpath = tmpdir.join("mod.py")
tmpdir.ensure("__init__.py") tmpdir.ensure("__init__.py")
modpath.write(source) modpath.write(source)

View File

@ -93,12 +93,12 @@ class TestFillFixtures:
sub1.join("conftest.py").write(_pytest._code.Source(""" sub1.join("conftest.py").write(_pytest._code.Source("""
import pytest import pytest
def pytest_funcarg__arg1(request): def pytest_funcarg__arg1(request):
pytest.raises(Exception, "request.getfuncargvalue('arg2')") pytest.raises(Exception, "request.getfixturevalue('arg2')")
""")) """))
sub2.join("conftest.py").write(_pytest._code.Source(""" sub2.join("conftest.py").write(_pytest._code.Source("""
import pytest import pytest
def pytest_funcarg__arg2(request): def pytest_funcarg__arg2(request):
pytest.raises(Exception, "request.getfuncargvalue('arg1')") pytest.raises(Exception, "request.getfixturevalue('arg1')")
""")) """))
sub1.join("test_in_sub1.py").write("def test_1(arg1): pass") sub1.join("test_in_sub1.py").write("def test_1(arg1): pass")
@ -435,21 +435,23 @@ class TestRequestBasic:
assert len(arg2fixturedefs) == 1 assert len(arg2fixturedefs) == 1
assert arg2fixturedefs[0].__name__ == "pytest_funcarg__something" assert arg2fixturedefs[0].__name__ == "pytest_funcarg__something"
def test_getfuncargvalue_recursive(self, testdir): def test_getfixturevalue_recursive(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
def pytest_funcarg__something(request): def pytest_funcarg__something(request):
return 1 return 1
""") """)
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__something(request): def pytest_funcarg__something(request):
return request.getfuncargvalue("something") + 1 return request.getfixturevalue("something") + 1
def test_func(something): def test_func(something):
assert something == 2 assert something == 2
""") """)
reprec = testdir.inline_run() reprec = testdir.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_getfuncargvalue(self, testdir): @pytest.mark.parametrize(
'getfixmethod', ('getfixturevalue', 'getfuncargvalue'))
def test_getfixturevalue(self, testdir, getfixmethod):
item = testdir.getitem(""" item = testdir.getitem("""
l = [2] l = [2]
def pytest_funcarg__something(request): return 1 def pytest_funcarg__something(request): return 1
@ -458,14 +460,15 @@ class TestRequestBasic:
def test_func(something): pass def test_func(something): pass
""") """)
req = item._request req = item._request
pytest.raises(FixtureLookupError, req.getfuncargvalue, "notexists") fixture_fetcher = getattr(req, getfixmethod)
val = req.getfuncargvalue("something") pytest.raises(FixtureLookupError, fixture_fetcher, "notexists")
val = fixture_fetcher("something")
assert val == 1 assert val == 1
val = req.getfuncargvalue("something") val = fixture_fetcher("something")
assert val == 1 assert val == 1
val2 = req.getfuncargvalue("other") val2 = fixture_fetcher("other")
assert val2 == 2 assert val2 == 2
val2 = req.getfuncargvalue("other") # see about caching val2 = fixture_fetcher("other") # see about caching
assert val2 == 2 assert val2 == 2
pytest._fillfuncargs(item) pytest._fillfuncargs(item)
assert item.funcargs["something"] == 1 assert item.funcargs["something"] == 1
@ -812,10 +815,10 @@ class TestRequestCachedSetup:
"*1 passed*" "*1 passed*"
]) ])
def test_request_cached_setup_getfuncargvalue(self, testdir): def test_request_cached_setup_getfixturevalue(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__arg1(request): def pytest_funcarg__arg1(request):
arg1 = request.getfuncargvalue("arg2") arg1 = request.getfixturevalue("arg2")
return request.cached_setup(lambda: arg1 + 1) return request.cached_setup(lambda: arg1 + 1)
def pytest_funcarg__arg2(request): def pytest_funcarg__arg2(request):
return request.cached_setup(lambda: 10) return request.cached_setup(lambda: 10)
@ -1118,7 +1121,7 @@ class TestFixtureUsages:
class TestFixtureManagerParseFactories: class TestFixtureManagerParseFactories:
def pytest_funcarg__testdir(self, request): def pytest_funcarg__testdir(self, request):
testdir = request.getfuncargvalue("testdir") testdir = request.getfixturevalue("testdir")
testdir.makeconftest(""" testdir.makeconftest("""
def pytest_funcarg__hello(request): def pytest_funcarg__hello(request):
return "conftest" return "conftest"
@ -1804,9 +1807,9 @@ class TestFixtureMarker:
reprec.assertoutcome(passed=4) reprec.assertoutcome(passed=4)
@pytest.mark.parametrize("method", [ @pytest.mark.parametrize("method", [
'request.getfuncargvalue("arg")', 'request.getfixturevalue("arg")',
'request.cached_setup(lambda: None, scope="function")', 'request.cached_setup(lambda: None, scope="function")',
], ids=["getfuncargvalue", "cached_setup"]) ], ids=["getfixturevalue", "cached_setup"])
def test_scope_mismatch_various(self, testdir, method): def test_scope_mismatch_various(self, testdir, method):
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest
@ -2737,6 +2740,7 @@ class TestContextManagerFixtureFuncs:
*def arg1* *def arg1*
""") """)
class TestParameterizedSubRequest: class TestParameterizedSubRequest:
def test_call_from_fixture(self, testdir): def test_call_from_fixture(self, testdir):
testfile = testdir.makepyfile(""" testfile = testdir.makepyfile("""
@ -2748,7 +2752,7 @@ class TestParameterizedSubRequest:
@pytest.fixture @pytest.fixture
def get_named_fixture(request): def get_named_fixture(request):
return request.getfuncargvalue('fix_with_param') return request.getfixturevalue('fix_with_param')
def test_foo(request, get_named_fixture): def test_foo(request, get_named_fixture):
pass pass
@ -2773,7 +2777,7 @@ class TestParameterizedSubRequest:
return request.param return request.param
def test_foo(request): def test_foo(request):
request.getfuncargvalue('fix_with_param') request.getfixturevalue('fix_with_param')
""") """)
result = testdir.runpytest() result = testdir.runpytest()
result.stdout.fnmatch_lines(""" result.stdout.fnmatch_lines("""
@ -2797,7 +2801,7 @@ class TestParameterizedSubRequest:
testfile = testdir.makepyfile(""" testfile = testdir.makepyfile("""
def test_foo(request): def test_foo(request):
request.getfuncargvalue('fix_with_param') request.getfixturevalue('fix_with_param')
""") """)
result = testdir.runpytest() result = testdir.runpytest()
result.stdout.fnmatch_lines(""" result.stdout.fnmatch_lines("""
@ -2827,7 +2831,7 @@ class TestParameterizedSubRequest:
from fix import fix_with_param from fix import fix_with_param
def test_foo(request): def test_foo(request):
request.getfuncargvalue('fix_with_param') request.getfixturevalue('fix_with_param')
""")) """))
tests_dir.chdir() tests_dir.chdir()
@ -2842,3 +2846,7 @@ class TestParameterizedSubRequest:
E*{1}:5 E*{1}:5
*1 failed* *1 failed*
""".format(fixfile.strpath, testfile.basename)) """.format(fixfile.strpath, testfile.basename))
def test_getfuncargvalue_is_deprecated(request):
pytest.deprecated_call(request.getfuncargvalue, 'tmpdir')

View File

@ -720,14 +720,14 @@ class TestMetafuncFunctional:
"*4 failed*", "*4 failed*",
]) ])
def test_parametrize_and_inner_getfuncargvalue(self, testdir): def test_parametrize_and_inner_getfixturevalue(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize("arg1", [1], indirect=True) metafunc.parametrize("arg1", [1], indirect=True)
metafunc.parametrize("arg2", [10], indirect=True) metafunc.parametrize("arg2", [10], indirect=True)
def pytest_funcarg__arg1(request): def pytest_funcarg__arg1(request):
x = request.getfuncargvalue("arg2") x = request.getfixturevalue("arg2")
return x + request.param return x + request.param
def pytest_funcarg__arg2(request): def pytest_funcarg__arg2(request):

View File

@ -8,7 +8,7 @@ def standalone(request):
class Standalone: class Standalone:
def __init__(self, request): def __init__(self, request):
self.testdir = request.getfuncargvalue("testdir") self.testdir = request.getfixturevalue("testdir")
script = "mypytest" script = "mypytest"
result = self.testdir.runpytest("--genscript=%s" % script) result = self.testdir.runpytest("--genscript=%s" % script)
assert result.ret == 0 assert result.ret == 0

View File

@ -13,7 +13,7 @@ def runpdb_and_get_report(testdir, source):
class TestPDB: class TestPDB:
def pytest_funcarg__pdblist(self, request): def pytest_funcarg__pdblist(self, request):
monkeypatch = request.getfuncargvalue("monkeypatch") monkeypatch = request.getfixturevalue("monkeypatch")
pdblist = [] pdblist = []
def mypdb(*args): def mypdb(*args):
pdblist.append(args) pdblist.append(args)