From ecc2cf6744aca61dc238d720370aa18015ac64a4 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 1 Aug 2017 20:49:07 -0300 Subject: [PATCH] Properly escape test names when setting PYTEST_CURRENT_TEST environment variable Fix #2644 --- _pytest/pytester.py | 12 ++++++++---- _pytest/runner.py | 7 ++++++- changelog/2644.bugfix | 1 + testing/acceptance_test.py | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 changelog/2644.bugfix diff --git a/_pytest/pytester.py b/_pytest/pytester.py index fc9b8d9cbe4..f6fcc1dfff7 100644 --- a/_pytest/pytester.py +++ b/_pytest/pytester.py @@ -379,13 +379,17 @@ def parseoutcomes(self): return d raise ValueError("Pytest terminal report not found") - def assert_outcomes(self, passed=0, skipped=0, failed=0): + def assert_outcomes(self, passed=0, skipped=0, failed=0, error=0): """ assert that the specified outcomes appear with the respective numbers (0 means it didn't occur) in the text output from a test run.""" d = self.parseoutcomes() - assert passed == d.get("passed", 0) - assert skipped == d.get("skipped", 0) - assert failed == d.get("failed", 0) + obtained = { + 'passed': d.get('passed', 0), + 'skipped': d.get('skipped', 0), + 'failed': d.get('failed', 0), + 'error': d.get('error', 0), + } + assert obtained == dict(passed=passed, skipped=skipped, failed=failed, error=error) class Testdir: diff --git a/_pytest/runner.py b/_pytest/runner.py index 003ce242f11..7572afa7c51 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -7,6 +7,7 @@ from time import time import py +from _pytest.compat import _ascii_escaped, _PY2 from _pytest._code.code import TerminalRepr, ExceptionInfo from _pytest.outcomes import skip, Skipped, TEST_OUTCOME @@ -134,7 +135,11 @@ def _update_current_test_var(item, when): """ var_name = 'PYTEST_CURRENT_TEST' if when: - os.environ[var_name] = '{0} ({1})'.format(item.nodeid, when) + value = _ascii_escaped('{0} ({1})'.format(item.nodeid, when)) + if _PY2: + # python 2 doesn't like null bytes on environment variables (see #2644) + value = value.replace('\x00', '(null)') + os.environ[var_name] = value else: os.environ.pop(var_name) diff --git a/changelog/2644.bugfix b/changelog/2644.bugfix new file mode 100644 index 00000000000..f70e7ec1bf8 --- /dev/null +++ b/changelog/2644.bugfix @@ -0,0 +1 @@ +Properly escape test names when setting ``PYTEST_CURRENT_TEST`` environment variable. diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 9a75a43e5f8..71277690641 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -400,7 +400,7 @@ def test_plugins_given_as_strings(self, tmpdir, monkeypatch): monkeypatch.setitem(sys.modules, 'myplugin', mod) assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0 - def test_parameterized_with_bytes_regex(self, testdir): + def test_parametrized_with_bytes_regex(self, testdir): p = testdir.makepyfile(""" import re import pytest @@ -414,6 +414,19 @@ def test_stuff(r): '*1 passed*' ]) + def test_parametrized_with_null_bytes(self, testdir): + """Test parametrization with values that contain null bytes and unicode characters (#2644)""" + p = testdir.makepyfile(u""" + # encoding: UTF-8 + import pytest + + @pytest.mark.parametrize("data", ["\\x00", u'ação']) + def test_foo(data): + assert data + """) + res = testdir.runpytest(p) + res.assert_outcomes(passed=2) + class TestInvocationVariants(object): def test_earlyinit(self, testdir):