From e1e4b226c6aa7ea81bf18eb192a6f979275d7097 Mon Sep 17 00:00:00 2001 From: Tomer Keren Date: Mon, 5 Nov 2018 10:13:37 +0200 Subject: [PATCH] :ok_hand: Address code review Edited the changelog for extra clarity, and to fire off auto-formatting Oddly enough, keeping `filename='{filename!r}'` caused an error while collecting tests, but getting rid of the single ticks fixed it Hopefully closes #3191 --- changelog/3191.feature.rst | 9 +++++++-- src/_pytest/assertion/rewrite.py | 2 +- testing/test_warnings.py | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/changelog/3191.feature.rst b/changelog/3191.feature.rst index d62301a0c..13d2049bf 100644 --- a/changelog/3191.feature.rst +++ b/changelog/3191.feature.rst @@ -3,9 +3,14 @@ A warning is now issued when assertions are made directly against ``None``. This is a common source of confusion among new users, which write:: assert mocked_object.assert_called_with(3, 4, 5, key='value') - -When they should write:: + +When they should write:: mocked_object.assert_called_with(3, 4, 5, key='value') Because the ``assert_called_with`` method of mock objects already executes an assertion. + +This warning will not be issued when ``None`` is explicitly checked + assert none_returning_fun() is None + +will not issue the warning diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 7603eb497..bfb81cf85 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -901,7 +901,7 @@ from warnings import warn_explicit warn_explicit( PytestWarning('assertion the value None, Please use "assert is None"'), category=None, - filename='{filename}', + filename={filename!r}, lineno={lineno}, ) """.format( diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 5369416ad..8fa1a96ef 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -623,6 +623,8 @@ def test_removed_in_pytest4_warning_as_error(testdir, change_default): else: assert change_default in ("ini", "cmdline") result.stdout.fnmatch_lines(["* 1 passed in *"]) + + class TestAssertionWarnings: @staticmethod def assert_result_warns(result): @@ -660,7 +662,6 @@ class TestAssertionWarnings: result = testdir.runpytest() self.assert_result_warns(result) - @pytest.mark.xfail(strict=True) 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( @@ -673,10 +674,9 @@ class TestAssertionWarnings: """ ) result = testdir.runpytest() - self.assert_result_warns(result) + result.stdout.fnmatch_lines(["*1 passed in*"]) - @pytest.mark.xfail(strict=True) def test_false_function_no_warn(self, testdir): self.create_file(testdir, False) result = testdir.runpytest() - self.assert_result_warns(result) + result.stdout.fnmatch_lines(["*1 failed in*"])