Skip to content

Commit

Permalink
fix addmarker - extract mark from markdecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Jun 12, 2018
1 parent 1b5322d commit e390323
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,14 @@ def _marked(func, mark):
class MarkInfo(object):
""" Marking object created by :class:`MarkDecorator` instances. """

_marks = attr.ib()
_marks = attr.ib(convert=list)

@_marks.validator
def validate_marks(self, attribute, value):
for item in value:
if not isinstance(item, Mark):
raise ValueError(item)

combined = attr.ib(
repr=False,
default=attr.Factory(
Expand Down
9 changes: 7 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ def listchain(self):
chain.reverse()
return chain

def add_marker(self, marker):
def add_marker(self, marker, append=True):
""" dynamically add a marker object to the node.
``marker`` can be a string or pytest.mark.* instance.
``append=True`` whether to append the martker,
if false insert at position 0
"""
from _pytest.mark import MarkDecorator, MARK_GEN

Expand All @@ -185,7 +187,10 @@ def add_marker(self, marker):
elif not isinstance(marker, MarkDecorator):
raise ValueError("is not a string or pytest.mark.* Marker")
self.keywords[marker.name] = marker
self.own_markers.append(marker)
if append:
self.own_markers.append(marker.mark)
else:
self.own_markers.insert(0, marker.mark)

def iter_markers(self, name=None):
"""
Expand Down
20 changes: 19 additions & 1 deletion testing/test_mark.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from __future__ import absolute_import, division, print_function
import os
import sys

import mock
import pytest
from _pytest.mark import (
MarkGenerator as Mark,
ParameterSet,
transfer_markers,
EMPTY_PARAMETERSET_OPTION,
)
from _pytest.nodes import Node

ignore_markinfo = pytest.mark.filterwarnings(
"ignore:MarkInfo objects:_pytest.deprecated.RemovedInPytest4Warning"
Expand Down Expand Up @@ -1123,3 +1124,20 @@ class TestBarClass(BaseTests):
passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes()
assert passed_k == 2
assert skipped_k == failed_k == 0


def test_addmarker_getmarker():
node = Node("Test", config=mock.Mock(), session=mock.Mock(), nodeid="Test")
node.add_marker(pytest.mark.a(1))
node.add_marker("b")
node.get_marker("a").combined
node.get_marker("b").combined


def test_addmarker_order():
node = Node("Test", config=mock.Mock(), session=mock.Mock(), nodeid="Test")
node.add_marker("a")
node.add_marker("b")
node.add_marker("c", append=False)
extracted = [x.name for x in node.iter_markers()]
assert extracted == ["c", "a", "b"]

0 comments on commit e390323

Please sign in to comment.