From d81b703f1091d912cb6901ff637254a6c591dc09 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 11 Oct 2013 14:36:54 +0200 Subject: [PATCH] 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. --- CHANGELOG | 10 ++++++++++ _pytest/mark.py | 7 +++++-- testing/test_mark.py | 11 +++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index de790fa46..1b414bc0a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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__", "") != "" 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("()") +