def test_multiple_patterns(self, pytester: Pytester): """Test support for multiple --doctest-glob arguments (#1255).""" pytester.maketxtfile(xdoc=""" >>> 1 1 """) pytester.makefile( ".foo", test=""" >>> 1 1 """, ) pytester.maketxtfile(test_normal=""" >>> 1 1 """) expected = {"xdoc.txt", "test.foo", "test_normal.txt"} assert {x.name for x in pytester.path.iterdir()} == expected args = ["--doctest-glob=xdoc*.txt", "--doctest-glob=*.foo"] result = pytester.runpytest(*args) result.stdout.fnmatch_lines( ["*test.foo *", "*xdoc.txt *", "*2 passed*"]) result = pytester.runpytest() result.stdout.fnmatch_lines(["*test_normal.txt *", "*1 passed*"])
def test_doctest_outcomes(self, pytester: Pytester): pytester.maketxtfile( test_skip=""" >>> 1 1 >>> import pytest >>> pytest.skip("") >>> 2 3 """, test_xfail=""" >>> import pytest >>> pytest.xfail("xfail_reason") >>> foo bar """, test_importorskip=""" >>> import pytest >>> pytest.importorskip("doesnotexist") >>> foo bar """, ) result = pytester.runpytest("--doctest-modules") result.stdout.fnmatch_lines([ "collected 3 items", "", "test_importorskip.txt s *", "test_skip.txt s *", "test_xfail.txt x *", "", "*= 2 skipped, 1 xfailed in *", ])
def test_doctest_unexpected_exception(self, pytester: Pytester): pytester.maketxtfile(""" >>> i = 0 >>> 0 / i 2 """) result = pytester.runpytest("--doctest-modules") result.stdout.fnmatch_lines( [ "test_doctest_unexpected_exception.txt F *", "", "*= FAILURES =*", "*_ [[]doctest[]] test_doctest_unexpected_exception.txt _*", "001 >>> i = 0", "002 >>> 0 / i", "UNEXPECTED EXCEPTION: ZeroDivisionError*", "Traceback (most recent call last):", ' File "*/doctest.py", line *, in __run', " *", ' File "<doctest test_doctest_unexpected_exception.txt[1]>", line 1, in <module>', "ZeroDivisionError: division by zero", "*/test_doctest_unexpected_exception.txt:2: UnexpectedException", ], consecutive=True, )
def test_number_and_allow_unicode(self, pytester: Pytester): pytester.maketxtfile(test_doc=""" >>> from collections import namedtuple >>> T = namedtuple('T', 'a b c') >>> T(a=0.2330000001, b=u'str', c=b'bytes') # doctest: +ALLOW_UNICODE, +ALLOW_BYTES, +NUMBER T(a=0.233, b=u'str', c='bytes') """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_bytes_literal(self, pytester: Pytester): """Test that doctests which output bytes fail in Python 3 when the ALLOW_BYTES option is not used. (#1287). """ pytester.maketxtfile(test_doc=""" >>> b'foo' 'foo' """) reprec = pytester.inline_run() reprec.assertoutcome(failed=1)
def test_unicode_string(self, pytester: Pytester): """Test that doctests which output unicode fail in Python 2 when the ALLOW_UNICODE option is not used. The same test should pass in Python 3. """ pytester.maketxtfile(test_doc=""" >>> b'12'.decode('ascii') '12' """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_doctest_unex_importerror_only_txt(self, pytester: Pytester): pytester.maketxtfile(""" >>> import asdalsdkjaslkdjasd >>> """) result = pytester.runpytest() # doctest is never executed because of error during hello.py collection result.stdout.fnmatch_lines([ "*>>> import asdals*", "*UNEXPECTED*ModuleNotFoundError*", "ModuleNotFoundError: No module named *asdal*", ])
def test_doctest_unex_importerror_with_module(self, pytester: Pytester): pytester.path.joinpath("hello.py").write_text( textwrap.dedent("""\ import asdalsdkjaslkdjasd """)) pytester.maketxtfile(""" >>> import hello >>> """) result = pytester.runpytest("--doctest-modules") # doctest is never executed because of error during hello.py collection result.stdout.fnmatch_lines([ "*ERROR collecting hello.py*", "*ModuleNotFoundError: No module named *asdals*", "*Interrupted: 1 error during collection*", ])
def test_txtfile_with_usefixtures_in_ini(self, pytester: Pytester): pytester.makeini( """ [pytest] usefixtures = myfixture """ ) pytester.makeconftest( """ import pytest @pytest.fixture def myfixture(monkeypatch): monkeypatch.setenv("HELLO", "WORLD") """ ) p = pytester.maketxtfile( """ >>> import os >>> os.environ["HELLO"] 'WORLD' """ ) reprec = pytester.inline_run(p) reprec.assertoutcome(passed=1)
def test_txtfile_with_fixtures(self, pytester: Pytester): p = pytester.maketxtfile(""" >>> p = getfixture('tmp_path') >>> p.is_dir() True """) reprec = pytester.inline_run(p) reprec.assertoutcome(passed=1)
def test_new_pattern(self, pytester: Pytester): p = pytester.maketxtfile(xdoc=""" >>> x = 1 >>> x == 1 False """) reprec = pytester.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1)
def test_simple_doctestfile(self, pytester: Pytester): p = pytester.maketxtfile(test_doc=""" >>> x = 1 >>> x == 1 False """) reprec = pytester.inline_run(p) reprec.assertoutcome(failed=1)
def test_collect_testtextfile(self, pytester: Pytester): w = pytester.maketxtfile(whatever="") checkfile = pytester.maketxtfile(test_something=""" alskdjalsdk >>> i = 5 >>> i-1 4 """) for x in (pytester.path, checkfile): # print "checking that %s returns custom items" % (x,) items, reprec = pytester.inline_genitems(x) assert len(items) == 1 assert isinstance(items[0], DoctestItem) assert isinstance(items[0].parent, DoctestTextfile) # Empty file has no items. items, reprec = pytester.inline_genitems(w) assert len(items) == 0
def test_continue_on_failure(self, pytester: Pytester): pytester.maketxtfile(test_something=""" >>> i = 5 >>> def foo(): ... raise ValueError('error1') >>> foo() >>> i >>> i + 2 7 >>> i + 1 """) result = pytester.runpytest("--doctest-modules", "--doctest-continue-on-failure") result.assert_outcomes(passed=0, failed=1) # The lines that contains the failure are 4, 5, and 8. The first one # is a stack trace and the other two are mismatches. result.stdout.fnmatch_lines([ "*4: UnexpectedException*", "*5: DocTestFailure*", "*8: DocTestFailure*" ])
def test_non_ignored_whitespace_glob(self, pytester: Pytester): pytester.makeini(""" [pytest] doctest_optionflags = ELLIPSIS """) p = pytester.maketxtfile(xdoc=""" >>> a = "foo " >>> print(a) foo """) reprec = pytester.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1, passed=0)
def test_doctest_items(pytester: Pytester) -> None: pytester.makepyfile( ''' def foo(): """ >>> 1 + 1 2 """ ''' ) pytester.maketxtfile( """ >>> 1 + 1 2 """ ) result = pytester.runpytest( "--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v" ) assert result.ret == 0 result.stdout.fnmatch_lines(["*collected 2 items*"])
def test_print_unicode_value(self, pytester: Pytester): """ Test case for issue 3583: Printing Unicode in doctest under Python 2.7 doesn't work """ p = pytester.maketxtfile(test_print_unicode_value=r""" Here is a doctest:: >>> print('\xE5\xE9\xEE\xF8\xFC') åéîøü """) result = pytester.runpytest(p) result.stdout.fnmatch_lines(["* 1 passed *"])
def test_txtfile_failing(self, pytester: Pytester): p = pytester.maketxtfile(""" >>> i = 0 >>> i + 1 2 """) result = pytester.runpytest(p, "-s") result.stdout.fnmatch_lines([ "001 >>> i = 0", "002 >>> i + 1", "Expected:", " 2", "Got:", " 1", "*test_txtfile_failing.txt:2: DocTestFailure", ])
def test_unicode_doctest(self, pytester: Pytester): """ Test case for issue 2434: DecodeError on Python 2 when doctest contains non-ascii characters. """ p = pytester.maketxtfile(test_unicode_doctest=""" .. doctest:: >>> print("Hi\\n\\nByé") Hi ... Byé >>> 1 / 0 # Byé 1 """) result = pytester.runpytest(p) result.stdout.fnmatch_lines( ["*UNEXPECTED EXCEPTION: ZeroDivisionError*", "*1 failed*"])