diff --git a/_pytest/doctest.py b/_pytest/doctest.py index ba5c082ec..fe71c8284 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -202,21 +202,26 @@ def _get_unicode_checker(): _literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE) - def _remove_u_prefixes(self, txt): - return re.sub(self._literal_re, r'\1\2', txt) - def check_output(self, want, got, optionflags): - res = doctest.OutputChecker.check_output(self, want, got, optionflags) + res = doctest.OutputChecker.check_output(self, want, got, + optionflags) if res: return True if not (optionflags & _get_allow_unicode_flag()): return False - cleaned_want = self._remove_u_prefixes(want) - cleaned_got = self._remove_u_prefixes(got) - res = doctest.OutputChecker.check_output(self, cleaned_want, cleaned_got, optionflags) - return res + else: # pragma: no cover + # the code below will end up executed only in Python 2 in + # our tests, and our coverage check runs in Python 3 only + def remove_u_prefixes(txt): + return re.sub(self._literal_re, r'\1\2', txt) + + want = remove_u_prefixes(want) + got = remove_u_prefixes(got) + res = doctest.OutputChecker.check_output(self, want, got, + optionflags) + return res _get_unicode_checker.UnicodeOutputChecker = UnicodeOutputChecker return _get_unicode_checker.UnicodeOutputChecker() diff --git a/testing/test_doctest.py b/testing/test_doctest.py index a5650afcf..6975ecc2c 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -432,16 +432,17 @@ class TestDoctests: reprec = testdir.inline_run("--doctest-modules") reprec.assertoutcome(passed=2) - @pytest.mark.skipif(sys.version_info[0] >= 3, reason='Python 2 only') - def test_unicode_string_fails(self, testdir): + def test_unicode_string(self, testdir): """Test that doctests which output unicode fail in Python 2 when - the ALLOW_UNICODE option is not used. + the ALLOW_UNICODE option is not used. The same test should pass + in Python 3. """ testdir.maketxtfile(test_doc=""" - >>> b'12'.decode('ascii') {comment} + >>> b'12'.decode('ascii') '12' """) reprec = testdir.inline_run() - reprec.assertoutcome(failed=1) + passed = int(sys.version_info[0] >= 3) + reprec.assertoutcome(passed=passed, failed=int(not passed))