From 491b30c5d97a807e2d885bc6127c6f7e08e41a82 Mon Sep 17 00:00:00 2001 From: Ceridwen Date: Fri, 1 Apr 2016 22:45:44 -0400 Subject: [PATCH] Add Hypothesis test for _idval and fix bug it found --- _pytest/python.py | 2 +- testing/python/metafunc.py | 17 +++++++++++++++++ testing/test_junitxml.py | 2 +- tox.ini | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 53e11a348..070c54715 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1122,7 +1122,7 @@ else: """ if isinstance(val, bytes): try: - return val.decode('ascii') + return val.encode('ascii') except UnicodeDecodeError: return val.encode('string-escape') else: diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index da4228ed8..59e05dd57 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -1,11 +1,18 @@ # -*- coding: utf-8 -*- import re +import sys import _pytest._code import py import pytest from _pytest import python as funcargs +import hypothesis +from hypothesis import strategies + +PY3 = sys.version_info >= (3, 0) + + class TestMetafunc: def Metafunc(self, func): # the unit tests of this class check if things work correctly @@ -121,6 +128,16 @@ class TestMetafunc: assert metafunc._calls[2].id == "x1-a" assert metafunc._calls[3].id == "x1-b" + @hypothesis.given(strategies.text() | strategies.binary()) + def test_idval_hypothesis(self, value): + from _pytest.python import _idval + escaped = _idval(value, 'a', 6, None) + assert isinstance(escaped, str) + if PY3: + escaped.encode('ascii') + else: + escaped.decode('ascii') + def test_unicode_idval(self): """This tests that Unicode strings outside the ASCII character set get escaped, using byte escapes if they're in that range or unicode diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 2436b60f5..8eda22f7f 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -610,7 +610,7 @@ def test_logxml_makedir(testdir): def test_escaped_parametrized_names_xml(testdir): testdir.makepyfile(""" import pytest - @pytest.mark.parametrize('char', ["\\x00"]) + @pytest.mark.parametrize('char', [u"\\x00"]) def test_func(char): assert char """) diff --git a/tox.ini b/tox.ini index 5f65446e4..82fe34b22 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,7 @@ envlist= commands= py.test --lsof -rfsxX {posargs:testing} passenv = USER USERNAME deps= + hypothesis nose mock requests