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 13, 2018
1 parent 4d0297b commit eb0c6a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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.
:type marker: str or pytest.mark.*
``append=True`` whether to append the marker,
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.mark)
if append:
self.own_markers.append(marker.mark)
else:
self.own_markers.insert(0, marker.mark)

def iter_markers(self, name=None):
"""
Expand Down
9 changes: 9 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,3 +1132,12 @@ def test_addmarker_getmarker():
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 eb0c6a8

Please sign in to comment.