Keep backward compatibility for code as kw in Node.warn

This commit is contained in:
Bruno Oliveira 2018-09-04 16:48:21 -03:00
parent d3ca739c00
commit b7560a8808
2 changed files with 18 additions and 5 deletions

View File

@ -136,7 +136,7 @@ class Node(object):
def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, getattr(self, "name", None))
def warn(self, code_or_warning, message=None):
def warn(self, code_or_warning=None, message=None, code=None):
"""Issue a warning for this item.
Warnings will be displayed after the test session, unless explicitly suppressed.
@ -164,12 +164,20 @@ class Node(object):
:param Union[Warning,str] code_or_warning: warning instance or warning code (legacy).
:param Union[str,None] message: message to display when called in the legacy form.
:param str code: code for the warning, in legacy form when using keyword arguments.
:return:
"""
if message is None:
if code_or_warning is None:
raise ValueError("code_or_warning must be given")
self._std_warn(code_or_warning)
else:
self._legacy_warn(code_or_warning, message)
if code_or_warning and code:
raise ValueError(
"code_or_warning and code cannot both be passed to this function"
)
code = code_or_warning or code
self._legacy_warn(code, message)
def _legacy_warn(self, code, message):
"""

View File

@ -809,18 +809,23 @@ class TestLegacyWarning(object):
)
@pytest.mark.filterwarnings("default")
def test_warn_on_test_item_from_request(self, testdir, request):
@pytest.mark.parametrize("use_kw", [True, False])
def test_warn_on_test_item_from_request(self, testdir, use_kw):
code_kw = "code=" if use_kw else ""
message_kw = "message=" if use_kw else ""
testdir.makepyfile(
"""
import pytest
@pytest.fixture
def fix(request):
request.node.warn("T1", "hello")
request.node.warn({code_kw}"T1", {message_kw}"hello")
def test_hello(fix):
pass
"""
""".format(
code_kw=code_kw, message_kw=message_kw
)
)
result = testdir.runpytest("--disable-pytest-warnings")
assert "hello" not in result.stdout.str()