Skip to content

Commit 8969bd4

Browse files
authored
Merge pull request #2646 from nicoddemus/issue-2644
Properly escape test names when setting PYTEST_CURRENT_TEST environment variable
2 parents 02da156 + 7703dc9 commit 8969bd4

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

_pytest/pytester.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,17 @@ def parseoutcomes(self):
381381
return d
382382
raise ValueError("Pytest terminal report not found")
383383

384-
def assert_outcomes(self, passed=0, skipped=0, failed=0):
384+
def assert_outcomes(self, passed=0, skipped=0, failed=0, error=0):
385385
""" assert that the specified outcomes appear with the respective
386386
numbers (0 means it didn't occur) in the text output from a test run."""
387387
d = self.parseoutcomes()
388-
assert passed == d.get("passed", 0)
389-
assert skipped == d.get("skipped", 0)
390-
assert failed == d.get("failed", 0)
388+
obtained = {
389+
'passed': d.get('passed', 0),
390+
'skipped': d.get('skipped', 0),
391+
'failed': d.get('failed', 0),
392+
'error': d.get('error', 0),
393+
}
394+
assert obtained == dict(passed=passed, skipped=skipped, failed=failed, error=error)
391395

392396

393397
class Testdir:

_pytest/runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from time import time
88

99
import py
10+
from _pytest.compat import _PY2
1011
from _pytest._code.code import TerminalRepr, ExceptionInfo
1112
from _pytest.outcomes import skip, Skipped, TEST_OUTCOME
1213

@@ -134,7 +135,11 @@ def _update_current_test_var(item, when):
134135
"""
135136
var_name = 'PYTEST_CURRENT_TEST'
136137
if when:
137-
os.environ[var_name] = '{0} ({1})'.format(item.nodeid, when)
138+
value = '{0} ({1})'.format(item.nodeid, when)
139+
if _PY2:
140+
# python 2 doesn't like null bytes on environment variables (see #2644)
141+
value = value.replace('\x00', '(null)')
142+
os.environ[var_name] = value
138143
else:
139144
os.environ.pop(var_name)
140145

changelog/2644.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Properly escape test names when setting ``PYTEST_CURRENT_TEST`` environment variable.

testing/acceptance_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_plugins_given_as_strings(self, tmpdir, monkeypatch):
400400
monkeypatch.setitem(sys.modules, 'myplugin', mod)
401401
assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0
402402

403-
def test_parameterized_with_bytes_regex(self, testdir):
403+
def test_parametrized_with_bytes_regex(self, testdir):
404404
p = testdir.makepyfile("""
405405
import re
406406
import pytest
@@ -414,6 +414,19 @@ def test_stuff(r):
414414
'*1 passed*'
415415
])
416416

417+
def test_parametrized_with_null_bytes(self, testdir):
418+
"""Test parametrization with values that contain null bytes and unicode characters (#2644)"""
419+
p = testdir.makepyfile(u"""
420+
# encoding: UTF-8
421+
import pytest
422+
423+
@pytest.mark.parametrize("data", ["\\x00", u'ação'])
424+
def test_foo(data):
425+
assert data
426+
""")
427+
res = testdir.runpytest(p)
428+
res.assert_outcomes(passed=2)
429+
417430

418431
class TestInvocationVariants(object):
419432
def test_earlyinit(self, testdir):

0 commit comments

Comments
 (0)