Skip to content

Commit

Permalink
fix #2540, introduce mark.with_args
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Jul 21, 2017
1 parent abb5d20 commit 65b2de1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
15 changes: 12 additions & 3 deletions _pytest/mark.py
Expand Up @@ -335,6 +335,17 @@ def __eq__(self, other):
def __repr__(self):
return "<MarkDecorator %r>" % (self.mark,)

def with_args(self, *args, **kwargs):
""" return a MarkDecorator with extra arguments added
unlike call this can be used even if the sole argument is a callable/class
:return: MarkDecorator
"""

mark = Mark(self.name, args, kwargs)
return self.__class__(self.mark.combined_with(mark))

def __call__(self, *args, **kwargs):
""" if passed a single callable argument: decorate it with mark info.
otherwise add *args/**kwargs in-place to mark information. """
Expand All @@ -348,9 +359,7 @@ def __call__(self, *args, **kwargs):
store_legacy_markinfo(func, self.mark)
store_mark(func, self.mark)
return func

mark = Mark(self.name, args, kwargs)
return self.__class__(self.mark.combined_with(mark))
return self.with_args(*args, **kwargs)


def get_unpacked_marks(obj):
Expand Down
1 change: 1 addition & 0 deletions changelog/2540.feature
@@ -0,0 +1 @@
Introduce ``mark.with_args`` in order to allow passing functions/classes as sole argument to marks.
13 changes: 13 additions & 0 deletions testing/test_mark.py
Expand Up @@ -22,6 +22,19 @@ def test_pytest_mark_notcallable(self):
mark = Mark()
pytest.raises((AttributeError, TypeError), mark)

def test_mark_with_param(self):
def some_function(abc):
pass

class SomeClass(object):
pass

assert pytest.mark.fun(some_function) is some_function
assert pytest.mark.fun.with_args(some_function) is not some_function

assert pytest.mark.fun(SomeClass) is SomeClass
assert pytest.mark.fun.with_args(SomeClass) is not SomeClass

def test_pytest_mark_name_starts_with_underscore(self):
mark = Mark()
pytest.raises(AttributeError, getattr, mark, '_some_name')
Expand Down

0 comments on commit 65b2de1

Please sign in to comment.