Skip to content

Commit

Permalink
test, reset in obj
Browse files Browse the repository at this point in the history
The test is not including keywords for now (in the end,
#359)
  • Loading branch information
blueyed committed Apr 9, 2020
1 parent b96b13c commit 2cadcfd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def obj(self):
@obj.setter
def obj(self, value) -> None:
self._obj = value
self._obj_markers = None

def _getobj(self):
"""Gets the underlying Python object. May be overwritten by subclasses."""
Expand Down
52 changes: 51 additions & 1 deletion testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from unittest import mock

import pytest
from _pytest.config import Config
from _pytest.config import ExitCode
from _pytest.mark import EMPTY_PARAMETERSET_OPTION
from _pytest.mark import MarkGenerator as Mark
from _pytest.mark.structures import NodeKeywords
from _pytest.nodes import Collector
from _pytest.nodes import Node
from _pytest.python import Function


class TestMark:
Expand Down Expand Up @@ -961,7 +964,7 @@ class TestBarClass(BaseTests):
# assert skipped_k == failed_k == 0


def test_addmarker_order():
def test_addmarker_order(pytestconfig: Config, monkeypatch) -> None:
session = mock.Mock()
session.own_markers = []
session.parent = None
Expand All @@ -973,6 +976,53 @@ def test_addmarker_order():
extracted = [x.name for x in node.iter_markers()]
assert extracted == ["baz", "foo", "bar"]

# Check marks/keywords with Function.
session.name = "session"
session.keywords = NodeKeywords(session)

# Register markers for `--strict-markers`.
added_markers = pytestconfig._inicache["markers"] + [
"funcmark",
"prepended",
"funcmark2",
]
monkeypatch.setitem(pytestconfig._inicache, "markers", added_markers)

@pytest.mark.funcmark
def f1():
assert False, "don't call me"

func = Function.from_parent(node, name="func", callobj=f1)
expected_marks = ["funcmark", "baz", "foo", "bar"]
assert [x.name for x in func.iter_markers()] == expected_marks
func.add_marker("prepended", append=False)
assert [x.name for x in func.iter_markers()] == ["prepended"] + expected_marks
assert set(func.keywords) == {
"Test",
"bar",
"baz",
"foo",
"func",
"funcmark",
"prepended",
"pytestmark",
"session",
}

# Changing the "obj" updates marks and keywords (lazily).
@pytest.mark.funcmark2
def f2():
assert False, "don't call me"

func.obj = f2
assert [x.name for x in func.iter_markers()] == [
"prepended",
"funcmark2",
"baz",
"foo",
"bar",
]


@pytest.mark.filterwarnings("ignore")
def test_markers_from_parametrize(testdir):
Expand Down

0 comments on commit 2cadcfd

Please sign in to comment.