diff options
author | holger krekel <holger@merlinux.eu> | 2013-10-11 14:36:54 +0200 |
---|---|---|
committer | holger krekel <holger@merlinux.eu> | 2013-10-11 14:36:54 +0200 |
commit | d81b703f1091d912cb6901ff637254a6c591dc09 (patch) | |
tree | c8f3edbedb367e95f3fee9b4119cfeeb44c67552 | |
parent | 1265cb99520b59f46ff7396adab41ae5319a34c5 (diff) | |
download | pytest-d81b703f1091d912cb6901ff637254a6c591dc09.tar.gz |
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.
-rw-r--r-- | CHANGELOG | 10 | ||||
-rw-r--r-- | _pytest/mark.py | 7 | ||||
-rw-r--r-- | testing/test_mark.py | 11 |
3 files changed, 26 insertions, 2 deletions
@@ -12,6 +12,16 @@ Changes between 2.4.2 and 2.4.3 properly so that the pkg_resources.resource_stream method works properly. 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 ----------------------------------- diff --git a/_pytest/mark.py b/_pytest/mark.py index 7fad6e9dd..cfeec6518 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -182,6 +182,9 @@ class MarkGenerator: if name not in self._markers: raise AttributeError("%r not a registered marker" % (name,)) +def istestfunc(func): + return hasattr(func, "__call__") and \ + getattr(func, "__name__", "<lambda>") != "<lambda>" class MarkDecorator: """ 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. """ if args: func = args[0] - if len(args) == 1 and hasattr(func, '__call__') or \ - hasattr(func, '__bases__'): + if len(args) == 1 and (istestfunc(func) or + hasattr(func, '__bases__')): if hasattr(func, '__bases__'): if hasattr(func, 'pytestmark'): l = func.pytestmark diff --git a/testing/test_mark.py b/testing/test_mark.py index 8aa87c729..818937061 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -100,6 +100,16 @@ def test_markers_option(testdir): "*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): testdir.makepyfile(""" @@ -510,3 +520,4 @@ class TestKeywordSelection: assert_test_is_not_selected("__") assert_test_is_not_selected("()") + |