avoid one surprising case of marker malfunction/confusion::

@pytest.mark.some(lambda arg: ...)
    def test_function():

would not work correctly because pytest assumes @pytest.mark.some
gets a function to be decorated already.  We now at least detect if this
arg is an lambda and thus the example will work.  Thanks Alex Gaynor
for bringing it up.
This commit is contained in:
holger krekel 2013-10-11 14:36:54 +02:00
parent 1265cb9952
commit d81b703f10
3 changed files with 26 additions and 2 deletions

View File

@ -12,6 +12,16 @@ Changes between 2.4.2 and 2.4.3
properly so that the pkg_resources.resource_stream method works properly. properly so that the pkg_resources.resource_stream method works properly.
Fixes issue366. Thanks for the investigations and full PR to Jason R. Coombs. Fixes issue366. Thanks for the investigations and full PR to Jason R. Coombs.
- avoid one surprising case of marker malfunction/confusion::
@pytest.mark.some(lambda arg: ...)
def test_function():
would not work correctly because pytest assumes @pytest.mark.some
gets a function to be decorated already. We now at least detect if this
arg is an lambda and thus the example will work. Thanks Alex Gaynor
for bringing it up.
Changes between 2.4.1 and 2.4.2 Changes between 2.4.1 and 2.4.2
----------------------------------- -----------------------------------

View File

@ -182,6 +182,9 @@ class MarkGenerator:
if name not in self._markers: if name not in self._markers:
raise AttributeError("%r not a registered marker" % (name,)) raise AttributeError("%r not a registered marker" % (name,))
def istestfunc(func):
return hasattr(func, "__call__") and \
getattr(func, "__name__", "<lambda>") != "<lambda>"
class MarkDecorator: class MarkDecorator:
""" A decorator for test functions and test classes. When applied """ A decorator for test functions and test classes. When applied
@ -217,8 +220,8 @@ class MarkDecorator:
otherwise add *args/**kwargs in-place to mark information. """ otherwise add *args/**kwargs in-place to mark information. """
if args: if args:
func = args[0] func = args[0]
if len(args) == 1 and hasattr(func, '__call__') or \ if len(args) == 1 and (istestfunc(func) or
hasattr(func, '__bases__'): hasattr(func, '__bases__')):
if hasattr(func, '__bases__'): if hasattr(func, '__bases__'):
if hasattr(func, 'pytestmark'): if hasattr(func, 'pytestmark'):
l = func.pytestmark l = func.pytestmark

View File

@ -100,6 +100,16 @@ def test_markers_option(testdir):
"*a1some*another marker", "*a1some*another marker",
]) ])
def test_mark_on_pseudo_function(testdir):
testdir.makepyfile("""
import pytest
@pytest.mark.r(lambda x: 0/0)
def test_hello():
pass
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_strict_prohibits_unregistered_markers(testdir): def test_strict_prohibits_unregistered_markers(testdir):
testdir.makepyfile(""" testdir.makepyfile("""
@ -510,3 +520,4 @@ class TestKeywordSelection:
assert_test_is_not_selected("__") assert_test_is_not_selected("__")
assert_test_is_not_selected("()") assert_test_is_not_selected("()")