Skip to content

Commit

Permalink
Properly escape test names when setting PYTEST_CURRENT_TEST environme…
Browse files Browse the repository at this point in the history
…nt variable

Fix pytest-dev#2644
  • Loading branch information
nicoddemus authored and fgmacedo committed Sep 4, 2017
1 parent 3875acf commit ecc2cf6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
12 changes: 8 additions & 4 deletions _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion _pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions changelog/2644.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Properly escape test names when setting ``PYTEST_CURRENT_TEST`` environment variable.
15 changes: 14 additions & 1 deletion testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit ecc2cf6

Please sign in to comment.