From e18b2a427a803a3e89b4dc88e6c904e32f20eb09 Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Wed, 11 Jan 2017 16:49:09 +0200 Subject: [PATCH 1/2] Fail assert_outcomes() on missing terminal report Currently if the terminal report of testdir.runpytest() is missing, assert_outcomes() on its output fails because parseoutcomes() returns an unexpected value (None). It's better to fail parseoutcomes() directly. --- AUTHORS | 1 + CHANGELOG.rst | 4 ++++ _pytest/pytester.py | 1 + testing/test_pytester.py | 7 +++++++ 4 files changed, 13 insertions(+) diff --git a/AUTHORS b/AUTHORS index e3bbdc5d1..acf715cde 100644 --- a/AUTHORS +++ b/AUTHORS @@ -141,3 +141,4 @@ Tyler Goodlet Vasily Kuznetsov Wouter van Ackooy Xuecong Liao +Eli Boyarski diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7c3df0327..f2248ca20 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,11 +14,15 @@ subdirectories with ini configuration files now uses the correct ini file (`#2148`_). Thanks `@pelme`_. +* Fail ``testdir.runpytest().assert_outcomes()`` explicitly if the pytest + terminal output it relies on is missing. Thanks to `@eli-b`_ for the PR. + * .. _@lesteve: https://github.com/lesteve .. _@malinoff: https://github.com/malinoff .. _@pelme: https://github.com/pelme +.. _@eli-b: https://github.com/eli-b .. _#2129: https://github.com/pytest-dev/pytest/issues/2129 .. _#2148: https://github.com/pytest-dev/pytest/issues/2148 diff --git a/_pytest/pytester.py b/_pytest/pytester.py index 17ff529a6..de7899173 100644 --- a/_pytest/pytester.py +++ b/_pytest/pytester.py @@ -367,6 +367,7 @@ class RunResult: for num, cat in outcomes: d[cat] = int(num) return d + raise ValueError("Pytest terminal report not found") def assert_outcomes(self, passed=0, skipped=0, failed=0): """ assert that the specified outcomes appear with the respective diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 14548808c..49cf43a3e 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -124,3 +124,10 @@ def test_inline_run_clean_modules(testdir): test_mod.write("def test_foo(): assert False") result2 = testdir.inline_run(str(test_mod)) assert result2.ret == EXIT_TESTSFAILED + +def test_assert_outcomes_after_pytest_erro(testdir): + testdir.makepyfile("def test_foo(): assert True") + + result = testdir.runpytest('--unexpected-argument') + with pytest.raises(ValueError, message="Pytest terminal report not found"): + result.assert_outcomes(passed=0) From c477f09177cabaaa1dbddd9d754f44ec9eba7b2a Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 19 Jan 2017 21:33:51 -0200 Subject: [PATCH 2/2] Assert statements of the pytester plugin again benefit from assertion rewriting Fix #1920 --- CHANGELOG.rst | 4 ++++ _pytest/config.py | 2 +- testing/test_assertion.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bb5c5e6a6..452d50b76 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,9 @@ expected warnings and the list of caught warnings is added to the error message. Thanks `@lesteve`_ for the PR. +* Assert statements of the ``pytester`` plugin again benefit from assertion rewriting (`#1920`_). + Thanks `@RonnyPfannschmidt`_ for the report and `@nicoddemus`_ for the PR. + * Specifying tests with colons like ``test_foo.py::test_bar`` for tests in subdirectories with ini configuration files now uses the correct ini file (`#2148`_). Thanks `@pelme`_. @@ -24,6 +27,7 @@ .. _@malinoff: https://github.com/malinoff .. _@pelme: https://github.com/pelme +.. _#1920: https://github.com/pytest-dev/pytest/issues/1920 .. _#2129: https://github.com/pytest-dev/pytest/issues/2129 .. _#2148: https://github.com/pytest-dev/pytest/issues/2148 .. _#2150: https://github.com/pytest-dev/pytest/issues/2150 diff --git a/_pytest/config.py b/_pytest/config.py index 8578e8aaa..42d1a118a 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -421,7 +421,7 @@ class PytestPluginManager(PluginManager): importspec = "_pytest." + modname else: importspec = modname - self.rewrite_hook.mark_rewrite(modname) + self.rewrite_hook.mark_rewrite(importspec) try: __import__(importspec) except ImportError as e: diff --git a/testing/test_assertion.py b/testing/test_assertion.py index c63f26b9c..9115d25e2 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -58,6 +58,23 @@ class TestImportHookInstallation: assert 0 result.stdout.fnmatch_lines([expected]) + def test_rewrite_assertions_pytester_plugin(self, testdir): + """ + Assertions in the pytester plugin must also benefit from assertion + rewriting (#1920). + """ + testdir.makepyfile(""" + pytest_plugins = ['pytester'] + def test_dummy_failure(testdir): # how meta! + testdir.makepyfile('def test(): assert 0') + r = testdir.inline_run() + r.assertoutcome(passed=1) + """) + result = testdir.runpytest_subprocess() + result.stdout.fnmatch_lines([ + '*assert 1 == 0*', + ]) + @pytest.mark.parametrize('mode', ['plain', 'rewrite']) def test_pytest_plugins_rewrite(self, testdir, mode): contents = {