From 5ebacc49c630178413a38deb6e344fc07f284be3 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 5 Dec 2018 19:22:44 +0100 Subject: [PATCH] Harden tests, fix doc/msg --- src/_pytest/assertion/rewrite.py | 11 ++++++----- testing/test_warnings.py | 13 ++++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 90724142a..78b8edcd8 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -880,12 +880,13 @@ class AssertionRewriter(ast.NodeVisitor): def warn_about_none_ast(self, node, module_path, lineno): """ - 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. - See issue #3191 for more details + 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 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 val_is_none = ast.Compare(node, [ast.Is()], [AST_NONE]) send_warning = ast.parse( @@ -893,7 +894,7 @@ class AssertionRewriter(ast.NodeVisitor): from _pytest.warning_types import PytestWarning from warnings import 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, filename={filename!r}, lineno={lineno}, diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 8fa1a96ef..655c89f4c 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -627,8 +627,8 @@ def test_removed_in_pytest4_warning_as_error(testdir, change_default): class TestAssertionWarnings: @staticmethod - def assert_result_warns(result): - result.stdout.fnmatch_lines(["*PytestWarning*"]) + def assert_result_warns(result, msg): + result.stdout.fnmatch_lines(["*PytestWarning: %s*" % msg]) def test_tuple_warning(self, testdir): testdir.makepyfile( @@ -638,7 +638,9 @@ class TestAssertionWarnings: """ ) result = testdir.runpytest() - self.assert_result_warns(result) + self.assert_result_warns( + result, "assertion is always true, perhaps remove parentheses?" + ) @staticmethod def create_file(testdir, return_none): @@ -660,10 +662,11 @@ class TestAssertionWarnings: def test_none_function_warns(self, testdir): self.create_file(testdir, True) 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): - """Tests a more simple case of `test_none_function_warns` where `assert None` is explicitly called""" testdir.makepyfile( """ def foo():