Harden tests, fix doc/msg
This commit is contained in:
parent
8a0ed7e2b3
commit
5ebacc49c6
|
@ -880,12 +880,13 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
|
|
||||||
def warn_about_none_ast(self, node, module_path, lineno):
|
def warn_about_none_ast(self, node, module_path, lineno):
|
||||||
"""
|
"""
|
||||||
Returns an ast issuing a warning if the value of node is `None`
|
Returns an AST issuing a warning if the value of node is `None`.
|
||||||
This is used to warn the user when asserting a function that asserts internally.
|
This is used to warn the user when asserting a function that asserts
|
||||||
See issue #3191 for more details
|
internally already.
|
||||||
|
See issue #3191 for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# using parse because it's different between py2 py3
|
# Using parse because it is different between py2 and py3.
|
||||||
AST_NONE = ast.parse("None").body[0].value
|
AST_NONE = ast.parse("None").body[0].value
|
||||||
val_is_none = ast.Compare(node, [ast.Is()], [AST_NONE])
|
val_is_none = ast.Compare(node, [ast.Is()], [AST_NONE])
|
||||||
send_warning = ast.parse(
|
send_warning = ast.parse(
|
||||||
|
@ -893,7 +894,7 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
from _pytest.warning_types import PytestWarning
|
from _pytest.warning_types import PytestWarning
|
||||||
from warnings import warn_explicit
|
from warnings import warn_explicit
|
||||||
warn_explicit(
|
warn_explicit(
|
||||||
PytestWarning('assertion the value None, Please use "assert is None"'),
|
PytestWarning('asserting the value None, please use "assert is None"'),
|
||||||
category=None,
|
category=None,
|
||||||
filename={filename!r},
|
filename={filename!r},
|
||||||
lineno={lineno},
|
lineno={lineno},
|
||||||
|
|
|
@ -627,8 +627,8 @@ def test_removed_in_pytest4_warning_as_error(testdir, change_default):
|
||||||
|
|
||||||
class TestAssertionWarnings:
|
class TestAssertionWarnings:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def assert_result_warns(result):
|
def assert_result_warns(result, msg):
|
||||||
result.stdout.fnmatch_lines(["*PytestWarning*"])
|
result.stdout.fnmatch_lines(["*PytestWarning: %s*" % msg])
|
||||||
|
|
||||||
def test_tuple_warning(self, testdir):
|
def test_tuple_warning(self, testdir):
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
|
@ -638,7 +638,9 @@ class TestAssertionWarnings:
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
self.assert_result_warns(result)
|
self.assert_result_warns(
|
||||||
|
result, "assertion is always true, perhaps remove parentheses?"
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_file(testdir, return_none):
|
def create_file(testdir, return_none):
|
||||||
|
@ -660,10 +662,11 @@ class TestAssertionWarnings:
|
||||||
def test_none_function_warns(self, testdir):
|
def test_none_function_warns(self, testdir):
|
||||||
self.create_file(testdir, True)
|
self.create_file(testdir, True)
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
self.assert_result_warns(result)
|
self.assert_result_warns(
|
||||||
|
result, 'asserting the value None, please use "assert is None"'
|
||||||
|
)
|
||||||
|
|
||||||
def test_assert_is_none_no_warn(self, testdir):
|
def test_assert_is_none_no_warn(self, testdir):
|
||||||
"""Tests a more simple case of `test_none_function_warns` where `assert None` is explicitly called"""
|
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
def foo():
|
def foo():
|
||||||
|
|
Loading…
Reference in New Issue