Skip to content

Commit

Permalink
Fix TypeError when first item in MarkInfo list of marks is a MarkDeco…
Browse files Browse the repository at this point in the history
…rator instance

Fix pytest-dev#3501
  • Loading branch information
nicoddemus committed Jun 12, 2018
1 parent 1b5322d commit 04703a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/3501.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow to insert ``pytest.mark.NAME`` (``MarkDecorator``) objects at the start of an item's marks.
19 changes: 13 additions & 6 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from operator import attrgetter

import attr
import six

from ..deprecated import MARK_PARAMETERSET_UNPACKING, MARK_INFO_ATTRIBUTE
from ..compat import NOTSET, getfslineno, MappingMixin
Expand Down Expand Up @@ -282,12 +283,18 @@ class MarkInfo(object):
""" Marking object created by :class:`MarkDecorator` instances. """

_marks = attr.ib()
combined = attr.ib(
repr=False,
default=attr.Factory(
lambda self: reduce(Mark.combined_with, self._marks), takes_self=True
),
)
combined = attr.ib(repr=False)

@combined.default
def _combined_default(self):
if six.PY2:
marks = [
(x.mark if isinstance(x, MarkDecorator) else x) for x in self._marks
]
else:
marks = self._marks
x = reduce(Mark.combined_with, marks)
return x

name = alias("combined.name", warning=MARK_INFO_ATTRIBUTE)
args = alias("combined.args", warning=MARK_INFO_ATTRIBUTE)
Expand Down
10 changes: 10 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ def g():
assert "reason" not in g.some.kwargs
assert g.some.kwargs["reason2"] == "456"

@ignore_markinfo
def test_pytest_mark_decorator_first_item(self):
"""Check that inserting a MarkDecorator as the first element of MarkInfo works (#3501)"""
from _pytest.mark.structures import MarkInfo, Mark

mark_info = MarkInfo(
[pytest.mark.slow(3), Mark(name="slow", args=(1,), kwargs={})]
)
assert mark_info.args == (3, 1)


def test_marked_class_run_twice(testdir, request):
"""Test fails file is run twice that contains marked class.
Expand Down

0 comments on commit 04703a2

Please sign in to comment.