| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from collections.abc import Callable |
| 5 |
import inspect |
| 6 |
from pathlib import Path |
| 7 |
import sys |
| 8 |
import textwrap |
| 9 |
|
| 10 |
from _pytest.doctest import _get_checker |
| 11 |
from _pytest.doctest import _is_main_py |
| 12 |
from _pytest.doctest import _is_mocked |
| 13 |
from _pytest.doctest import _is_setup_py |
| 14 |
from _pytest.doctest import _patch_unwrap_mock_aware |
| 15 |
from _pytest.doctest import DoctestItem |
| 16 |
from _pytest.doctest import DoctestModule |
| 17 |
from _pytest.doctest import DoctestTextfile |
| 18 |
from _pytest.pytester import Pytester |
| 19 |
import pytest |
| 20 |
|
| 21 |
|
| 22 |
class TestDoctests: |
| 23 |
def test_collect_testtextfile(self, pytester: Pytester): |
| 24 |
w = pytester.maketxtfile(whatever="") |
| 25 |
checkfile = pytester.maketxtfile( |
| 26 |
test_something=""" |
| 27 |
alskdjalsdk |
| 28 |
>>> i = 5 |
| 29 |
>>> i-1 |
| 30 |
4 |
| 31 |
""" |
| 32 |
) |
| 33 |
|
| 34 |
for x in (pytester.path, checkfile): |
| 35 |
# print "checking that %s returns custom items" % (x,) |
| 36 |
items, _reprec = pytester.inline_genitems(x) |
| 37 |
assert len(items) == 1 |
| 38 |
assert isinstance(items[0], DoctestItem) |
| 39 |
assert isinstance(items[0].parent, DoctestTextfile) |
| 40 |
# Empty file has no items. |
| 41 |
items, _reprec = pytester.inline_genitems(w) |
| 42 |
assert len(items) == 0 |
| 43 |
|
| 44 |
def test_collect_module_empty(self, pytester: Pytester): |
| 45 |
path = pytester.makepyfile(whatever="#") |
| 46 |
for p in (path, pytester.path): |
| 47 |
items, _reprec = pytester.inline_genitems(p, "--doctest-modules") |
| 48 |
assert len(items) == 0 |
| 49 |
|
| 50 |
def test_collect_module_single_modulelevel_doctest(self, pytester: Pytester): |
| 51 |
path = pytester.makepyfile(whatever='""">>> pass"""') |
| 52 |
for p in (path, pytester.path): |
| 53 |
items, _reprec = pytester.inline_genitems(p, "--doctest-modules") |
| 54 |
assert len(items) == 1 |
| 55 |
assert isinstance(items[0], DoctestItem) |
| 56 |
assert isinstance(items[0].parent, DoctestModule) |
| 57 |
|
| 58 |
def test_collect_module_two_doctest_one_modulelevel(self, pytester: Pytester): |
| 59 |
path = pytester.makepyfile( |
| 60 |
whatever=""" |
| 61 |
'>>> x = None' |
| 62 |
def my_func(): |
| 63 |
">>> magic = 42 " |
| 64 |
""" |
| 65 |
) |
| 66 |
for p in (path, pytester.path): |
| 67 |
items, _reprec = pytester.inline_genitems(p, "--doctest-modules") |
| 68 |
assert len(items) == 2 |
| 69 |
assert isinstance(items[0], DoctestItem) |
| 70 |
assert isinstance(items[1], DoctestItem) |
| 71 |
assert isinstance(items[0].parent, DoctestModule) |
| 72 |
assert items[0].parent is items[1].parent |
| 73 |
|
| 74 |
@pytest.mark.parametrize("filename", ["__init__", "whatever"]) |
| 75 |
def test_collect_module_two_doctest_no_modulelevel( |
| 76 |
self, |
| 77 |
pytester: Pytester, |
| 78 |
filename: str, |
| 79 |
) -> None: |
| 80 |
path = pytester.makepyfile( |
| 81 |
**{ |
| 82 |
filename: """ |
| 83 |
'# Empty' |
| 84 |
def my_func(): |
| 85 |
">>> magic = 42 " |
| 86 |
def useless(): |
| 87 |
''' |
| 88 |
# This is a function |
| 89 |
# >>> # it doesn't have any doctest |
| 90 |
''' |
| 91 |
def another(): |
| 92 |
''' |
| 93 |
# This is another function |
| 94 |
>>> import os # this one does have a doctest |
| 95 |
''' |
| 96 |
""", |
| 97 |
}, |
| 98 |
) |
| 99 |
for p in (path, pytester.path): |
| 100 |
items, _reprec = pytester.inline_genitems(p, "--doctest-modules") |
| 101 |
assert len(items) == 2 |
| 102 |
assert isinstance(items[0], DoctestItem) |
| 103 |
assert isinstance(items[1], DoctestItem) |
| 104 |
assert isinstance(items[0].parent, DoctestModule) |
| 105 |
assert items[0].parent is items[1].parent |
| 106 |
|
| 107 |
def test_simple_doctestfile(self, pytester: Pytester): |
| 108 |
p = pytester.maketxtfile( |
| 109 |
test_doc=""" |
| 110 |
>>> x = 1 |
| 111 |
>>> x == 1 |
| 112 |
False |
| 113 |
""" |
| 114 |
) |
| 115 |
reprec = pytester.inline_run(p) |
| 116 |
reprec.assertoutcome(failed=1) |
| 117 |
|
| 118 |
def test_importmode(self, pytester: Pytester): |
| 119 |
pytester.makepyfile( |
| 120 |
**{ |
| 121 |
"src/namespacepkg/innerpkg/__init__.py": "", |
| 122 |
"src/namespacepkg/innerpkg/a.py": """ |
| 123 |
def some_func(): |
| 124 |
return 42 |
| 125 |
""", |
| 126 |
"src/namespacepkg/innerpkg/b.py": """ |
| 127 |
from namespacepkg.innerpkg.a import some_func |
| 128 |
def my_func(): |
| 129 |
''' |
| 130 |
>>> my_func() |
| 131 |
42 |
| 132 |
''' |
| 133 |
return some_func() |
| 134 |
""", |
| 135 |
} |
| 136 |
) |
| 137 |
# For 'namespacepkg' to be considered a namespace package, its containing directory |
| 138 |
# needs to be reachable from sys.path: |
| 139 |
# https://packaging.python.org/en/latest/guides/packaging-namespace-packages |
| 140 |
pytester.syspathinsert(pytester.path / "src") |
| 141 |
reprec = pytester.inline_run("--doctest-modules", "--import-mode=importlib") |
| 142 |
reprec.assertoutcome(passed=1) |
| 143 |
|
| 144 |
def test_new_pattern(self, pytester: Pytester): |
| 145 |
p = pytester.maketxtfile( |
| 146 |
xdoc=""" |
| 147 |
>>> x = 1 |
| 148 |
>>> x == 1 |
| 149 |
False |
| 150 |
""" |
| 151 |
) |
| 152 |
reprec = pytester.inline_run(p, "--doctest-glob=x*.txt") |
| 153 |
reprec.assertoutcome(failed=1) |
| 154 |
|
| 155 |
def test_multiple_patterns(self, pytester: Pytester): |
| 156 |
"""Test support for multiple --doctest-glob arguments (#1255).""" |
| 157 |
pytester.maketxtfile( |
| 158 |
xdoc=""" |
| 159 |
>>> 1 |
| 160 |
1 |
| 161 |
""" |
| 162 |
) |
| 163 |
pytester.makefile( |
| 164 |
".foo", |
| 165 |
test=""" |
| 166 |
>>> 1 |
| 167 |
1 |
| 168 |
""", |
| 169 |
) |
| 170 |
pytester.maketxtfile( |
| 171 |
test_normal=""" |
| 172 |
>>> 1 |
| 173 |
1 |
| 174 |
""" |
| 175 |
) |
| 176 |
expected = {"xdoc.txt", "test.foo", "test_normal.txt"} |
| 177 |
assert {x.name for x in pytester.path.iterdir()} == expected |
| 178 |
args = ["--doctest-glob=xdoc*.txt", "--doctest-glob=*.foo"] |
| 179 |
result = pytester.runpytest(*args) |
| 180 |
result.stdout.fnmatch_lines(["*test.foo *", "*xdoc.txt *", "*2 passed*"]) |
| 181 |
result = pytester.runpytest() |
| 182 |
result.stdout.fnmatch_lines(["*test_normal.txt *", "*1 passed*"]) |
| 183 |
|
| 184 |
@pytest.mark.parametrize( |
| 185 |
" test_string, encoding", |
| 186 |
[("foo", "ascii"), ("öäü", "latin1"), ("öäü", "utf-8")], |
| 187 |
) |
| 188 |
def test_encoding(self, pytester, test_string, encoding): |
| 189 |
"""Test support for doctest_encoding ini option.""" |
| 190 |
pytester.makeini( |
| 191 |
f""" |
| 192 |
[pytest] |
| 193 |
doctest_encoding={encoding} |
| 194 |
""" |
| 195 |
) |
| 196 |
doctest = f""" |
| 197 |
>>> "{test_string}" |
| 198 |
{test_string!r} |
| 199 |
""" |
| 200 |
fn = pytester.path / "test_encoding.txt" |
| 201 |
fn.write_text(doctest, encoding=encoding) |
| 202 |
|
| 203 |
result = pytester.runpytest() |
| 204 |
|
| 205 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 206 |
|
| 207 |
def test_doctest_unexpected_exception(self, pytester: Pytester): |
| 208 |
pytester.maketxtfile( |
| 209 |
""" |
| 210 |
>>> i = 0 |
| 211 |
>>> 0 / i |
| 212 |
2 |
| 213 |
""" |
| 214 |
) |
| 215 |
result = pytester.runpytest("--doctest-modules") |
| 216 |
result.stdout.fnmatch_lines( |
| 217 |
[ |
| 218 |
"test_doctest_unexpected_exception.txt F *", |
| 219 |
"", |
| 220 |
"*= FAILURES =*", |
| 221 |
"*_ [[]doctest[]] test_doctest_unexpected_exception.txt _*", |
| 222 |
"001 >>> i = 0", |
| 223 |
"002 >>> 0 / i", |
| 224 |
"UNEXPECTED EXCEPTION: ZeroDivisionError*", |
| 225 |
"Traceback (most recent call last):", |
| 226 |
*( |
| 227 |
(' File "*/doctest.py", line *, in __run', " *") |
| 228 |
if sys.version_info <= (3, 14) |
| 229 |
else () |
| 230 |
), |
| 231 |
*((" *^^^^*", " *", " *") if sys.version_info[:2] == (3, 13) else ()), |
| 232 |
' File "<doctest test_doctest_unexpected_exception.txt[1]>", line 1, in <module>', |
| 233 |
"ZeroDivisionError: division by zero", |
| 234 |
"*/test_doctest_unexpected_exception.txt:2: UnexpectedException", |
| 235 |
], |
| 236 |
consecutive=True, |
| 237 |
) |
| 238 |
|
| 239 |
def test_doctest_outcomes(self, pytester: Pytester): |
| 240 |
pytester.maketxtfile( |
| 241 |
test_skip=""" |
| 242 |
>>> 1 |
| 243 |
1 |
| 244 |
>>> import pytest |
| 245 |
>>> pytest.skip("") |
| 246 |
>>> 2 |
| 247 |
3 |
| 248 |
""", |
| 249 |
test_xfail=""" |
| 250 |
>>> import pytest |
| 251 |
>>> pytest.xfail("xfail_reason") |
| 252 |
>>> foo |
| 253 |
bar |
| 254 |
""", |
| 255 |
test_importorskip=""" |
| 256 |
>>> import pytest |
| 257 |
>>> pytest.importorskip("doesnotexist") |
| 258 |
>>> foo |
| 259 |
bar |
| 260 |
""", |
| 261 |
) |
| 262 |
result = pytester.runpytest("--doctest-modules") |
| 263 |
result.stdout.fnmatch_lines( |
| 264 |
[ |
| 265 |
"collected 3 items", |
| 266 |
"", |
| 267 |
"test_importorskip.txt s *", |
| 268 |
"test_skip.txt s *", |
| 269 |
"test_xfail.txt x *", |
| 270 |
"", |
| 271 |
"*= 2 skipped, 1 xfailed in *", |
| 272 |
] |
| 273 |
) |
| 274 |
|
| 275 |
def test_docstring_partial_context_around_error(self, pytester: Pytester): |
| 276 |
"""Test that we show some context before the actual line of a failing |
| 277 |
doctest. |
| 278 |
""" |
| 279 |
pytester.makepyfile( |
| 280 |
''' |
| 281 |
def foo(): |
| 282 |
""" |
| 283 |
text-line-1 |
| 284 |
text-line-2 |
| 285 |
text-line-3 |
| 286 |
text-line-4 |
| 287 |
text-line-5 |
| 288 |
text-line-6 |
| 289 |
text-line-7 |
| 290 |
text-line-8 |
| 291 |
text-line-9 |
| 292 |
text-line-10 |
| 293 |
text-line-11 |
| 294 |
>>> 1 + 1 |
| 295 |
3 |
| 296 |
|
| 297 |
text-line-after |
| 298 |
""" |
| 299 |
''' |
| 300 |
) |
| 301 |
result = pytester.runpytest("--doctest-modules") |
| 302 |
result.stdout.fnmatch_lines( |
| 303 |
[ |
| 304 |
"*docstring_partial_context_around_error*", |
| 305 |
"005*text-line-3", |
| 306 |
"006*text-line-4", |
| 307 |
"013*text-line-11", |
| 308 |
"014*>>> 1 + 1", |
| 309 |
"Expected:", |
| 310 |
" 3", |
| 311 |
"Got:", |
| 312 |
" 2", |
| 313 |
] |
| 314 |
) |
| 315 |
# lines below should be trimmed out |
| 316 |
result.stdout.no_fnmatch_line("*text-line-2*") |
| 317 |
result.stdout.no_fnmatch_line("*text-line-after*") |
| 318 |
|
| 319 |
def test_docstring_full_context_around_error(self, pytester: Pytester): |
| 320 |
"""Test that we show the whole context before the actual line of a failing |
| 321 |
doctest, provided that the context is up to 10 lines long. |
| 322 |
""" |
| 323 |
pytester.makepyfile( |
| 324 |
''' |
| 325 |
def foo(): |
| 326 |
""" |
| 327 |
text-line-1 |
| 328 |
text-line-2 |
| 329 |
|
| 330 |
>>> 1 + 1 |
| 331 |
3 |
| 332 |
""" |
| 333 |
''' |
| 334 |
) |
| 335 |
result = pytester.runpytest("--doctest-modules") |
| 336 |
result.stdout.fnmatch_lines( |
| 337 |
[ |
| 338 |
"*docstring_full_context_around_error*", |
| 339 |
"003*text-line-1", |
| 340 |
"004*text-line-2", |
| 341 |
"006*>>> 1 + 1", |
| 342 |
"Expected:", |
| 343 |
" 3", |
| 344 |
"Got:", |
| 345 |
" 2", |
| 346 |
] |
| 347 |
) |
| 348 |
|
| 349 |
def test_doctest_linedata_missing(self, pytester: Pytester): |
| 350 |
pytester.path.joinpath("hello.py").write_text( |
| 351 |
textwrap.dedent( |
| 352 |
"""\ |
| 353 |
class Fun(object): |
| 354 |
@property |
| 355 |
def test(self): |
| 356 |
''' |
| 357 |
>>> a = 1 |
| 358 |
>>> 1/0 |
| 359 |
''' |
| 360 |
""" |
| 361 |
), |
| 362 |
encoding="utf-8", |
| 363 |
) |
| 364 |
result = pytester.runpytest("--doctest-modules") |
| 365 |
result.stdout.fnmatch_lines( |
| 366 |
["*hello*", "006*>>> 1/0*", "*UNEXPECTED*ZeroDivision*", "*1 failed*"] |
| 367 |
) |
| 368 |
|
| 369 |
def test_doctest_linedata_on_property(self, pytester: Pytester): |
| 370 |
pytester.makepyfile( |
| 371 |
""" |
| 372 |
class Sample(object): |
| 373 |
@property |
| 374 |
def some_property(self): |
| 375 |
''' |
| 376 |
>>> Sample().some_property |
| 377 |
'another thing' |
| 378 |
''' |
| 379 |
return 'something' |
| 380 |
""" |
| 381 |
) |
| 382 |
result = pytester.runpytest("--doctest-modules") |
| 383 |
result.stdout.fnmatch_lines( |
| 384 |
[ |
| 385 |
"*= FAILURES =*", |
| 386 |
"*_ [[]doctest[]] test_doctest_linedata_on_property.Sample.some_property _*", |
| 387 |
"004 ", |
| 388 |
"005 *>>> Sample().some_property", |
| 389 |
"Expected:", |
| 390 |
" 'another thing'", |
| 391 |
"Got:", |
| 392 |
" 'something'", |
| 393 |
"", |
| 394 |
"*/test_doctest_linedata_on_property.py:5: DocTestFailure", |
| 395 |
"*= 1 failed in *", |
| 396 |
] |
| 397 |
) |
| 398 |
|
| 399 |
def test_doctest_no_linedata_on_overridden_property(self, pytester: Pytester): |
| 400 |
pytester.makepyfile( |
| 401 |
""" |
| 402 |
class Sample(object): |
| 403 |
@property |
| 404 |
def some_property(self): |
| 405 |
''' |
| 406 |
>>> Sample().some_property |
| 407 |
'another thing' |
| 408 |
''' |
| 409 |
return 'something' |
| 410 |
some_property = property(some_property.__get__, None, None, some_property.__doc__) |
| 411 |
""" |
| 412 |
) |
| 413 |
result = pytester.runpytest("--doctest-modules") |
| 414 |
result.stdout.fnmatch_lines( |
| 415 |
[ |
| 416 |
"*= FAILURES =*", |
| 417 |
"*_ [[]doctest[]] test_doctest_no_linedata_on_overridden_property.Sample.some_property _*", |
| 418 |
"EXAMPLE LOCATION UNKNOWN, not showing all tests of that example", |
| 419 |
"[?][?][?] >>> Sample().some_property", |
| 420 |
"Expected:", |
| 421 |
" 'another thing'", |
| 422 |
"Got:", |
| 423 |
" 'something'", |
| 424 |
"", |
| 425 |
"*/test_doctest_no_linedata_on_overridden_property.py:None: DocTestFailure", |
| 426 |
"*= 1 failed in *", |
| 427 |
] |
| 428 |
) |
| 429 |
|
| 430 |
def test_doctest_unex_importerror_only_txt(self, pytester: Pytester): |
| 431 |
pytester.maketxtfile( |
| 432 |
""" |
| 433 |
>>> import asdalsdkjaslkdjasd |
| 434 |
>>> |
| 435 |
""" |
| 436 |
) |
| 437 |
result = pytester.runpytest() |
| 438 |
# doctest is never executed because of error during hello.py collection |
| 439 |
result.stdout.fnmatch_lines( |
| 440 |
[ |
| 441 |
"*>>> import asdals*", |
| 442 |
"*UNEXPECTED*ModuleNotFoundError*", |
| 443 |
"ModuleNotFoundError: No module named *asdal*", |
| 444 |
] |
| 445 |
) |
| 446 |
|
| 447 |
def test_doctest_unex_importerror_with_module(self, pytester: Pytester): |
| 448 |
pytester.path.joinpath("hello.py").write_text( |
| 449 |
textwrap.dedent( |
| 450 |
"""\ |
| 451 |
import asdalsdkjaslkdjasd |
| 452 |
""" |
| 453 |
), |
| 454 |
encoding="utf-8", |
| 455 |
) |
| 456 |
pytester.maketxtfile( |
| 457 |
""" |
| 458 |
>>> import hello |
| 459 |
>>> |
| 460 |
""" |
| 461 |
) |
| 462 |
result = pytester.runpytest("--doctest-modules") |
| 463 |
# doctest is never executed because of error during hello.py collection |
| 464 |
result.stdout.fnmatch_lines( |
| 465 |
[ |
| 466 |
"*ERROR collecting hello.py*", |
| 467 |
"*ModuleNotFoundError: No module named *asdals*", |
| 468 |
"*Interrupted: 1 error during collection*", |
| 469 |
] |
| 470 |
) |
| 471 |
|
| 472 |
def test_doctestmodule(self, pytester: Pytester): |
| 473 |
p = pytester.makepyfile( |
| 474 |
""" |
| 475 |
''' |
| 476 |
>>> x = 1 |
| 477 |
>>> x == 1 |
| 478 |
False |
| 479 |
|
| 480 |
''' |
| 481 |
""" |
| 482 |
) |
| 483 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 484 |
reprec.assertoutcome(failed=1) |
| 485 |
|
| 486 |
def test_doctest_cached_property(self, pytester: Pytester): |
| 487 |
p = pytester.makepyfile( |
| 488 |
""" |
| 489 |
import functools |
| 490 |
|
| 491 |
class Foo: |
| 492 |
@functools.cached_property |
| 493 |
def foo(self): |
| 494 |
''' |
| 495 |
>>> assert False, "Tacos!" |
| 496 |
''' |
| 497 |
... |
| 498 |
""" |
| 499 |
) |
| 500 |
result = pytester.runpytest(p, "--doctest-modules") |
| 501 |
result.assert_outcomes(failed=1) |
| 502 |
assert "Tacos!" in result.stdout.str() |
| 503 |
|
| 504 |
def test_doctestmodule_external_and_issue116(self, pytester: Pytester): |
| 505 |
p = pytester.mkpydir("hello") |
| 506 |
p.joinpath("__init__.py").write_text( |
| 507 |
textwrap.dedent( |
| 508 |
"""\ |
| 509 |
def somefunc(): |
| 510 |
''' |
| 511 |
>>> i = 0 |
| 512 |
>>> i + 1 |
| 513 |
2 |
| 514 |
''' |
| 515 |
""" |
| 516 |
), |
| 517 |
encoding="utf-8", |
| 518 |
) |
| 519 |
result = pytester.runpytest(p, "--doctest-modules") |
| 520 |
result.stdout.fnmatch_lines( |
| 521 |
[ |
| 522 |
"003 *>>> i = 0", |
| 523 |
"004 *>>> i + 1", |
| 524 |
"*Expected:", |
| 525 |
"* 2", |
| 526 |
"*Got:", |
| 527 |
"* 1", |
| 528 |
"*:4: DocTestFailure", |
| 529 |
] |
| 530 |
) |
| 531 |
|
| 532 |
def test_txtfile_failing(self, pytester: Pytester): |
| 533 |
p = pytester.maketxtfile( |
| 534 |
""" |
| 535 |
>>> i = 0 |
| 536 |
>>> i + 1 |
| 537 |
2 |
| 538 |
""" |
| 539 |
) |
| 540 |
result = pytester.runpytest(p, "-s") |
| 541 |
result.stdout.fnmatch_lines( |
| 542 |
[ |
| 543 |
"001 >>> i = 0", |
| 544 |
"002 >>> i + 1", |
| 545 |
"Expected:", |
| 546 |
" 2", |
| 547 |
"Got:", |
| 548 |
" 1", |
| 549 |
"*test_txtfile_failing.txt:2: DocTestFailure", |
| 550 |
] |
| 551 |
) |
| 552 |
|
| 553 |
def test_txtfile_with_fixtures(self, pytester: Pytester): |
| 554 |
p = pytester.maketxtfile( |
| 555 |
""" |
| 556 |
>>> p = getfixture('tmp_path') |
| 557 |
>>> p.is_dir() |
| 558 |
True |
| 559 |
""" |
| 560 |
) |
| 561 |
reprec = pytester.inline_run(p) |
| 562 |
reprec.assertoutcome(passed=1) |
| 563 |
|
| 564 |
def test_txtfile_with_usefixtures_in_ini(self, pytester: Pytester): |
| 565 |
pytester.makeini( |
| 566 |
""" |
| 567 |
[pytest] |
| 568 |
usefixtures = myfixture |
| 569 |
""" |
| 570 |
) |
| 571 |
pytester.makeconftest( |
| 572 |
""" |
| 573 |
import pytest |
| 574 |
@pytest.fixture |
| 575 |
def myfixture(monkeypatch): |
| 576 |
monkeypatch.setenv("HELLO", "WORLD") |
| 577 |
""" |
| 578 |
) |
| 579 |
|
| 580 |
p = pytester.maketxtfile( |
| 581 |
""" |
| 582 |
>>> import os |
| 583 |
>>> os.environ["HELLO"] |
| 584 |
'WORLD' |
| 585 |
""" |
| 586 |
) |
| 587 |
reprec = pytester.inline_run(p) |
| 588 |
reprec.assertoutcome(passed=1) |
| 589 |
|
| 590 |
def test_doctestmodule_with_fixtures(self, pytester: Pytester): |
| 591 |
p = pytester.makepyfile( |
| 592 |
""" |
| 593 |
''' |
| 594 |
>>> p = getfixture('tmp_path') |
| 595 |
>>> p.is_dir() |
| 596 |
True |
| 597 |
''' |
| 598 |
""" |
| 599 |
) |
| 600 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 601 |
reprec.assertoutcome(passed=1) |
| 602 |
|
| 603 |
def test_module_fixture_available_to_normal_test_with_doctestmodules( |
| 604 |
self, pytester: Pytester |
| 605 |
) -> None: |
| 606 |
"""Regression test for #14533. |
| 607 |
|
| 608 |
Module-level fixtures collected with ``--doctest-modules`` are available |
| 609 |
both to normal tests and doctests in the same file. |
| 610 |
""" |
| 611 |
pytester.makepyfile( |
| 612 |
""" |
| 613 |
import pytest |
| 614 |
|
| 615 |
@pytest.fixture |
| 616 |
def fix(): |
| 617 |
return "fix" |
| 618 |
|
| 619 |
def test(fix): |
| 620 |
assert fix == "fix" |
| 621 |
|
| 622 |
def func(): |
| 623 |
'''My function. |
| 624 |
|
| 625 |
>>> getfixture("fix") |
| 626 |
'fix' |
| 627 |
''' |
| 628 |
""" |
| 629 |
) |
| 630 |
|
| 631 |
result = pytester.runpytest("--doctest-modules") |
| 632 |
result.assert_outcomes(passed=2) |
| 633 |
|
| 634 |
def test_doctestmodule_three_tests(self, pytester: Pytester): |
| 635 |
p = pytester.makepyfile( |
| 636 |
""" |
| 637 |
''' |
| 638 |
>>> p = getfixture('tmp_path') |
| 639 |
>>> p.is_dir() |
| 640 |
True |
| 641 |
''' |
| 642 |
def my_func(): |
| 643 |
''' |
| 644 |
>>> magic = 42 |
| 645 |
>>> magic - 42 |
| 646 |
0 |
| 647 |
''' |
| 648 |
def useless(): |
| 649 |
pass |
| 650 |
def another(): |
| 651 |
''' |
| 652 |
>>> import os |
| 653 |
>>> os is os |
| 654 |
True |
| 655 |
''' |
| 656 |
""" |
| 657 |
) |
| 658 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 659 |
reprec.assertoutcome(passed=3) |
| 660 |
|
| 661 |
def test_doctestmodule_two_tests_one_fail(self, pytester: Pytester): |
| 662 |
p = pytester.makepyfile( |
| 663 |
""" |
| 664 |
class MyClass(object): |
| 665 |
def bad_meth(self): |
| 666 |
''' |
| 667 |
>>> magic = 42 |
| 668 |
>>> magic |
| 669 |
0 |
| 670 |
''' |
| 671 |
def nice_meth(self): |
| 672 |
''' |
| 673 |
>>> magic = 42 |
| 674 |
>>> magic - 42 |
| 675 |
0 |
| 676 |
''' |
| 677 |
""" |
| 678 |
) |
| 679 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 680 |
reprec.assertoutcome(failed=1, passed=1) |
| 681 |
|
| 682 |
def test_ignored_whitespace(self, pytester: Pytester): |
| 683 |
pytester.makeini( |
| 684 |
""" |
| 685 |
[pytest] |
| 686 |
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE |
| 687 |
""" |
| 688 |
) |
| 689 |
p = pytester.makepyfile( |
| 690 |
""" |
| 691 |
class MyClass(object): |
| 692 |
''' |
| 693 |
>>> a = "foo " |
| 694 |
>>> print(a) |
| 695 |
foo |
| 696 |
''' |
| 697 |
pass |
| 698 |
""" |
| 699 |
) |
| 700 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 701 |
reprec.assertoutcome(passed=1) |
| 702 |
|
| 703 |
def test_non_ignored_whitespace(self, pytester: Pytester): |
| 704 |
pytester.makeini( |
| 705 |
""" |
| 706 |
[pytest] |
| 707 |
doctest_optionflags = ELLIPSIS |
| 708 |
""" |
| 709 |
) |
| 710 |
p = pytester.makepyfile( |
| 711 |
""" |
| 712 |
class MyClass(object): |
| 713 |
''' |
| 714 |
>>> a = "foo " |
| 715 |
>>> print(a) |
| 716 |
foo |
| 717 |
''' |
| 718 |
pass |
| 719 |
""" |
| 720 |
) |
| 721 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 722 |
reprec.assertoutcome(failed=1, passed=0) |
| 723 |
|
| 724 |
def test_ignored_whitespace_glob(self, pytester: Pytester): |
| 725 |
pytester.makeini( |
| 726 |
""" |
| 727 |
[pytest] |
| 728 |
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE |
| 729 |
""" |
| 730 |
) |
| 731 |
p = pytester.maketxtfile( |
| 732 |
xdoc=""" |
| 733 |
>>> a = "foo " |
| 734 |
>>> print(a) |
| 735 |
foo |
| 736 |
""" |
| 737 |
) |
| 738 |
reprec = pytester.inline_run(p, "--doctest-glob=x*.txt") |
| 739 |
reprec.assertoutcome(passed=1) |
| 740 |
|
| 741 |
def test_non_ignored_whitespace_glob(self, pytester: Pytester): |
| 742 |
pytester.makeini( |
| 743 |
""" |
| 744 |
[pytest] |
| 745 |
doctest_optionflags = ELLIPSIS |
| 746 |
""" |
| 747 |
) |
| 748 |
p = pytester.maketxtfile( |
| 749 |
xdoc=""" |
| 750 |
>>> a = "foo " |
| 751 |
>>> print(a) |
| 752 |
foo |
| 753 |
""" |
| 754 |
) |
| 755 |
reprec = pytester.inline_run(p, "--doctest-glob=x*.txt") |
| 756 |
reprec.assertoutcome(failed=1, passed=0) |
| 757 |
|
| 758 |
def test_contains_unicode(self, pytester: Pytester): |
| 759 |
"""Fix internal error with docstrings containing non-ascii characters.""" |
| 760 |
pytester.makepyfile( |
| 761 |
'''\ |
| 762 |
def foo(): |
| 763 |
""" |
| 764 |
>>> name = 'с' # not letter 'c' but instead Cyrillic 's'. |
| 765 |
'anything' |
| 766 |
""" |
| 767 |
''' # noqa: RUF001 |
| 768 |
) |
| 769 |
result = pytester.runpytest("--doctest-modules") |
| 770 |
result.stdout.fnmatch_lines(["Got nothing", "* 1 failed in*"]) |
| 771 |
|
| 772 |
def test_ignore_import_errors_on_doctest(self, pytester: Pytester): |
| 773 |
p = pytester.makepyfile( |
| 774 |
""" |
| 775 |
import asdf |
| 776 |
|
| 777 |
def add_one(x): |
| 778 |
''' |
| 779 |
>>> add_one(1) |
| 780 |
2 |
| 781 |
''' |
| 782 |
return x + 1 |
| 783 |
""" |
| 784 |
) |
| 785 |
|
| 786 |
reprec = pytester.inline_run( |
| 787 |
p, "--doctest-modules", "--doctest-ignore-import-errors" |
| 788 |
) |
| 789 |
reprec.assertoutcome(skipped=1, failed=1, passed=0) |
| 790 |
|
| 791 |
def test_junit_report_for_doctest(self, pytester: Pytester): |
| 792 |
"""#713: Fix --junit-xml option when used with --doctest-modules.""" |
| 793 |
p = pytester.makepyfile( |
| 794 |
""" |
| 795 |
def foo(): |
| 796 |
''' |
| 797 |
>>> 1 + 1 |
| 798 |
3 |
| 799 |
''' |
| 800 |
pass |
| 801 |
""" |
| 802 |
) |
| 803 |
reprec = pytester.inline_run(p, "--doctest-modules", "--junit-xml=junit.xml") |
| 804 |
reprec.assertoutcome(failed=1) |
| 805 |
|
| 806 |
def test_unicode_doctest(self, pytester: Pytester): |
| 807 |
""" |
| 808 |
Test case for issue 2434: DecodeError on Python 2 when doctest contains non-ascii |
| 809 |
characters. |
| 810 |
""" |
| 811 |
p = pytester.maketxtfile( |
| 812 |
test_unicode_doctest=""" |
| 813 |
.. doctest:: |
| 814 |
|
| 815 |
>>> print("Hi\\n\\nByé") |
| 816 |
Hi |
| 817 |
... |
| 818 |
Byé |
| 819 |
>>> 1 / 0 # Byé |
| 820 |
1 |
| 821 |
""" |
| 822 |
) |
| 823 |
result = pytester.runpytest(p) |
| 824 |
result.stdout.fnmatch_lines( |
| 825 |
["*UNEXPECTED EXCEPTION: ZeroDivisionError*", "*1 failed*"] |
| 826 |
) |
| 827 |
|
| 828 |
def test_unicode_doctest_module(self, pytester: Pytester): |
| 829 |
""" |
| 830 |
Test case for issue 2434: DecodeError on Python 2 when doctest docstring |
| 831 |
contains non-ascii characters. |
| 832 |
""" |
| 833 |
p = pytester.makepyfile( |
| 834 |
test_unicode_doctest_module=""" |
| 835 |
def fix_bad_unicode(text): |
| 836 |
''' |
| 837 |
>>> print(fix_bad_unicode('único')) |
| 838 |
único |
| 839 |
''' |
| 840 |
return "único" |
| 841 |
""" |
| 842 |
) |
| 843 |
result = pytester.runpytest(p, "--doctest-modules") |
| 844 |
result.stdout.fnmatch_lines(["* 1 passed *"]) |
| 845 |
|
| 846 |
def test_print_unicode_value(self, pytester: Pytester): |
| 847 |
""" |
| 848 |
Test case for issue 3583: Printing Unicode in doctest under Python 2.7 |
| 849 |
doesn't work |
| 850 |
""" |
| 851 |
p = pytester.maketxtfile( |
| 852 |
test_print_unicode_value=r""" |
| 853 |
Here is a doctest:: |
| 854 |
|
| 855 |
>>> print('\xE5\xE9\xEE\xF8\xFC') |
| 856 |
åéîøü |
| 857 |
""" |
| 858 |
) |
| 859 |
result = pytester.runpytest(p) |
| 860 |
result.stdout.fnmatch_lines(["* 1 passed *"]) |
| 861 |
|
| 862 |
def test_reportinfo(self, pytester: Pytester): |
| 863 |
"""Make sure that DoctestItem.reportinfo() returns lineno.""" |
| 864 |
p = pytester.makepyfile( |
| 865 |
test_reportinfo=""" |
| 866 |
def foo(x): |
| 867 |
''' |
| 868 |
>>> foo('a') |
| 869 |
'b' |
| 870 |
''' |
| 871 |
return 'c' |
| 872 |
""" |
| 873 |
) |
| 874 |
items, _reprec = pytester.inline_genitems(p, "--doctest-modules") |
| 875 |
reportinfo = items[0].reportinfo() |
| 876 |
assert reportinfo[1] == 1 |
| 877 |
|
| 878 |
def test_valid_setup_py(self, pytester: Pytester): |
| 879 |
""" |
| 880 |
Test to make sure that pytest ignores valid setup.py files when ran |
| 881 |
with --doctest-modules |
| 882 |
""" |
| 883 |
p = pytester.makepyfile( |
| 884 |
setup=""" |
| 885 |
if __name__ == '__main__': |
| 886 |
from setuptools import setup, find_packages |
| 887 |
setup(name='sample', |
| 888 |
version='0.0', |
| 889 |
description='description', |
| 890 |
packages=find_packages() |
| 891 |
) |
| 892 |
""" |
| 893 |
) |
| 894 |
result = pytester.runpytest(p, "--doctest-modules") |
| 895 |
result.stdout.fnmatch_lines(["*collected 0 items*"]) |
| 896 |
|
| 897 |
def test_main_py_does_not_cause_import_errors(self, pytester: Pytester): |
| 898 |
p = pytester.copy_example("doctest/main_py") |
| 899 |
result = pytester.runpytest(p, "--doctest-modules") |
| 900 |
result.stdout.fnmatch_lines(["*collected 2 items*", "*1 failed, 1 passed*"]) |
| 901 |
|
| 902 |
def test_invalid_setup_py(self, pytester: Pytester): |
| 903 |
""" |
| 904 |
Test to make sure that pytest reads setup.py files that are not used |
| 905 |
for python packages when ran with --doctest-modules |
| 906 |
""" |
| 907 |
p = pytester.makepyfile( |
| 908 |
setup=""" |
| 909 |
def test_foo(): |
| 910 |
return 'bar' |
| 911 |
""" |
| 912 |
) |
| 913 |
result = pytester.runpytest(p, "--doctest-modules") |
| 914 |
result.stdout.fnmatch_lines(["*collected 1 item*"]) |
| 915 |
|
| 916 |
def test_setup_module(self, pytester: Pytester) -> None: |
| 917 |
"""Regression test for #12011 - setup_module not executed when running |
| 918 |
with `--doctest-modules`.""" |
| 919 |
pytester.makepyfile( |
| 920 |
""" |
| 921 |
CONSTANT = 0 |
| 922 |
|
| 923 |
def setup_module(): |
| 924 |
global CONSTANT |
| 925 |
CONSTANT = 1 |
| 926 |
|
| 927 |
def test(): |
| 928 |
assert CONSTANT == 1 |
| 929 |
""" |
| 930 |
) |
| 931 |
result = pytester.runpytest("--doctest-modules") |
| 932 |
assert result.ret == 0 |
| 933 |
result.assert_outcomes(passed=1) |
| 934 |
|
| 935 |
|
| 936 |
class TestLiterals: |
| 937 |
@pytest.mark.parametrize("config_mode", ["ini", "comment"]) |
| 938 |
def test_allow_unicode(self, pytester, config_mode): |
| 939 |
"""Test that doctests which output unicode work in all python versions |
| 940 |
tested by pytest when the ALLOW_UNICODE option is used (either in |
| 941 |
the configuration file or by an inline comment). |
| 942 |
""" |
| 943 |
if config_mode == "ini": |
| 944 |
pytester.makeini( |
| 945 |
""" |
| 946 |
[pytest] |
| 947 |
doctest_optionflags = ALLOW_UNICODE |
| 948 |
""" |
| 949 |
) |
| 950 |
comment = "" |
| 951 |
else: |
| 952 |
comment = "#doctest: +ALLOW_UNICODE" |
| 953 |
|
| 954 |
pytester.maketxtfile( |
| 955 |
test_doc=f""" |
| 956 |
>>> b'12'.decode('ascii') {comment} |
| 957 |
'12' |
| 958 |
""" |
| 959 |
) |
| 960 |
pytester.makepyfile( |
| 961 |
foo=f""" |
| 962 |
def foo(): |
| 963 |
''' |
| 964 |
>>> b'12'.decode('ascii') {comment} |
| 965 |
'12' |
| 966 |
''' |
| 967 |
""" |
| 968 |
) |
| 969 |
reprec = pytester.inline_run("--doctest-modules") |
| 970 |
reprec.assertoutcome(passed=2) |
| 971 |
|
| 972 |
@pytest.mark.parametrize("config_mode", ["ini", "comment"]) |
| 973 |
def test_allow_bytes(self, pytester, config_mode): |
| 974 |
"""Test that doctests which output bytes work in all python versions |
| 975 |
tested by pytest when the ALLOW_BYTES option is used (either in |
| 976 |
the configuration file or by an inline comment)(#1287). |
| 977 |
""" |
| 978 |
if config_mode == "ini": |
| 979 |
pytester.makeini( |
| 980 |
""" |
| 981 |
[pytest] |
| 982 |
doctest_optionflags = ALLOW_BYTES |
| 983 |
""" |
| 984 |
) |
| 985 |
comment = "" |
| 986 |
else: |
| 987 |
comment = "#doctest: +ALLOW_BYTES" |
| 988 |
|
| 989 |
pytester.maketxtfile( |
| 990 |
test_doc=f""" |
| 991 |
>>> b'foo' {comment} |
| 992 |
'foo' |
| 993 |
""" |
| 994 |
) |
| 995 |
pytester.makepyfile( |
| 996 |
foo=f""" |
| 997 |
def foo(): |
| 998 |
''' |
| 999 |
>>> b'foo' {comment} |
| 1000 |
'foo' |
| 1001 |
''' |
| 1002 |
""" |
| 1003 |
) |
| 1004 |
reprec = pytester.inline_run("--doctest-modules") |
| 1005 |
reprec.assertoutcome(passed=2) |
| 1006 |
|
| 1007 |
def test_unicode_string(self, pytester: Pytester): |
| 1008 |
"""Test that doctests which output unicode fail in Python 2 when |
| 1009 |
the ALLOW_UNICODE option is not used. The same test should pass |
| 1010 |
in Python 3. |
| 1011 |
""" |
| 1012 |
pytester.maketxtfile( |
| 1013 |
test_doc=""" |
| 1014 |
>>> b'12'.decode('ascii') |
| 1015 |
'12' |
| 1016 |
""" |
| 1017 |
) |
| 1018 |
reprec = pytester.inline_run() |
| 1019 |
reprec.assertoutcome(passed=1) |
| 1020 |
|
| 1021 |
def test_bytes_literal(self, pytester: Pytester): |
| 1022 |
"""Test that doctests which output bytes fail in Python 3 when |
| 1023 |
the ALLOW_BYTES option is not used. (#1287). |
| 1024 |
""" |
| 1025 |
pytester.maketxtfile( |
| 1026 |
test_doc=""" |
| 1027 |
>>> b'foo' |
| 1028 |
'foo' |
| 1029 |
""" |
| 1030 |
) |
| 1031 |
reprec = pytester.inline_run() |
| 1032 |
reprec.assertoutcome(failed=1) |
| 1033 |
|
| 1034 |
def test_number_re(self) -> None: |
| 1035 |
_number_re = _get_checker()._number_re # type: ignore |
| 1036 |
for s in [ |
| 1037 |
"1.", |
| 1038 |
"+1.", |
| 1039 |
"-1.", |
| 1040 |
".1", |
| 1041 |
"+.1", |
| 1042 |
"-.1", |
| 1043 |
"0.1", |
| 1044 |
"+0.1", |
| 1045 |
"-0.1", |
| 1046 |
"1e5", |
| 1047 |
"+1e5", |
| 1048 |
"1e+5", |
| 1049 |
"+1e+5", |
| 1050 |
"1e-5", |
| 1051 |
"+1e-5", |
| 1052 |
"-1e-5", |
| 1053 |
"1.2e3", |
| 1054 |
"-1.2e-3", |
| 1055 |
]: |
| 1056 |
print(s) |
| 1057 |
m = _number_re.match(s) |
| 1058 |
assert m is not None |
| 1059 |
assert float(m.group()) == pytest.approx(float(s)) |
| 1060 |
for s in ["1", "abc"]: |
| 1061 |
print(s) |
| 1062 |
assert _number_re.match(s) is None |
| 1063 |
|
| 1064 |
@pytest.mark.parametrize("config_mode", ["ini", "comment"]) |
| 1065 |
def test_number_precision(self, pytester, config_mode): |
| 1066 |
"""Test the NUMBER option.""" |
| 1067 |
if config_mode == "ini": |
| 1068 |
pytester.makeini( |
| 1069 |
""" |
| 1070 |
[pytest] |
| 1071 |
doctest_optionflags = NUMBER |
| 1072 |
""" |
| 1073 |
) |
| 1074 |
comment = "" |
| 1075 |
else: |
| 1076 |
comment = "#doctest: +NUMBER" |
| 1077 |
|
| 1078 |
pytester.maketxtfile( |
| 1079 |
test_doc=f""" |
| 1080 |
|
| 1081 |
Scalars: |
| 1082 |
|
| 1083 |
>>> import math |
| 1084 |
>>> math.pi {comment} |
| 1085 |
3.141592653589793 |
| 1086 |
>>> math.pi {comment} |
| 1087 |
3.1416 |
| 1088 |
>>> math.pi {comment} |
| 1089 |
3.14 |
| 1090 |
>>> -math.pi {comment} |
| 1091 |
-3.14 |
| 1092 |
>>> math.pi {comment} |
| 1093 |
3. |
| 1094 |
>>> 3. {comment} |
| 1095 |
3.0 |
| 1096 |
>>> 3. {comment} |
| 1097 |
3. |
| 1098 |
>>> 3. {comment} |
| 1099 |
3.01 |
| 1100 |
>>> 3. {comment} |
| 1101 |
2.99 |
| 1102 |
>>> .299 {comment} |
| 1103 |
.3 |
| 1104 |
>>> .301 {comment} |
| 1105 |
.3 |
| 1106 |
>>> 951. {comment} |
| 1107 |
1e3 |
| 1108 |
>>> 1049. {comment} |
| 1109 |
1e3 |
| 1110 |
>>> -1049. {comment} |
| 1111 |
-1e3 |
| 1112 |
>>> 1e3 {comment} |
| 1113 |
1e3 |
| 1114 |
>>> 1e3 {comment} |
| 1115 |
1000. |
| 1116 |
|
| 1117 |
Lists: |
| 1118 |
|
| 1119 |
>>> [3.1415, 0.097, 13.1, 7, 8.22222e5, 0.598e-2] {comment} |
| 1120 |
[3.14, 0.1, 13., 7, 8.22e5, 6.0e-3] |
| 1121 |
>>> [[0.333, 0.667], [0.999, 1.333]] {comment} |
| 1122 |
[[0.33, 0.667], [0.999, 1.333]] |
| 1123 |
>>> [[[0.101]]] {comment} |
| 1124 |
[[[0.1]]] |
| 1125 |
|
| 1126 |
Doesn't barf on non-numbers: |
| 1127 |
|
| 1128 |
>>> 'abc' {comment} |
| 1129 |
'abc' |
| 1130 |
>>> None {comment} |
| 1131 |
""" |
| 1132 |
) |
| 1133 |
reprec = pytester.inline_run() |
| 1134 |
reprec.assertoutcome(passed=1) |
| 1135 |
|
| 1136 |
@pytest.mark.parametrize( |
| 1137 |
"expression,output", |
| 1138 |
[ |
| 1139 |
# ints shouldn't match floats: |
| 1140 |
("3.0", "3"), |
| 1141 |
("3e0", "3"), |
| 1142 |
("1e3", "1000"), |
| 1143 |
("3", "3.0"), |
| 1144 |
# Rounding: |
| 1145 |
("3.1", "3.0"), |
| 1146 |
("3.1", "3.2"), |
| 1147 |
("3.1", "4.0"), |
| 1148 |
("8.22e5", "810000.0"), |
| 1149 |
# Only the actual output is rounded up, not the expected output: |
| 1150 |
("3.0", "2.98"), |
| 1151 |
("1e3", "999"), |
| 1152 |
# The current implementation doesn't understand that numbers inside |
| 1153 |
# strings shouldn't be treated as numbers: |
| 1154 |
pytest.param("'3.1416'", "'3.14'", marks=pytest.mark.xfail), |
| 1155 |
], |
| 1156 |
) |
| 1157 |
def test_number_non_matches(self, pytester, expression, output): |
| 1158 |
pytester.maketxtfile( |
| 1159 |
test_doc=f""" |
| 1160 |
>>> {expression} #doctest: +NUMBER |
| 1161 |
{output} |
| 1162 |
""" |
| 1163 |
) |
| 1164 |
reprec = pytester.inline_run() |
| 1165 |
reprec.assertoutcome(passed=0, failed=1) |
| 1166 |
|
| 1167 |
def test_number_and_allow_unicode(self, pytester: Pytester): |
| 1168 |
pytester.maketxtfile( |
| 1169 |
test_doc=""" |
| 1170 |
>>> from collections import namedtuple |
| 1171 |
>>> T = namedtuple('T', 'a b c') |
| 1172 |
>>> T(a=0.2330000001, b=u'str', c=b'bytes') # doctest: +ALLOW_UNICODE, +ALLOW_BYTES, +NUMBER |
| 1173 |
T(a=0.233, b=u'str', c='bytes') |
| 1174 |
""" |
| 1175 |
) |
| 1176 |
reprec = pytester.inline_run() |
| 1177 |
reprec.assertoutcome(passed=1) |
| 1178 |
|
| 1179 |
|
| 1180 |
class TestDoctestSkips: |
| 1181 |
""" |
| 1182 |
If all examples in a doctest are skipped due to the SKIP option, then |
| 1183 |
the tests should be SKIPPED rather than PASSED. (#957) |
| 1184 |
""" |
| 1185 |
|
| 1186 |
@pytest.fixture(params=["text", "module"]) |
| 1187 |
def makedoctest(self, pytester, request): |
| 1188 |
def makeit(doctest): |
| 1189 |
mode = request.param |
| 1190 |
if mode == "text": |
| 1191 |
pytester.maketxtfile(doctest) |
| 1192 |
else: |
| 1193 |
assert mode == "module" |
| 1194 |
pytester.makepyfile(f'"""\n{doctest}"""') |
| 1195 |
|
| 1196 |
return makeit |
| 1197 |
|
| 1198 |
def test_one_skipped(self, pytester, makedoctest): |
| 1199 |
makedoctest( |
| 1200 |
""" |
| 1201 |
>>> 1 + 1 # doctest: +SKIP |
| 1202 |
2 |
| 1203 |
>>> 2 + 2 |
| 1204 |
4 |
| 1205 |
""" |
| 1206 |
) |
| 1207 |
reprec = pytester.inline_run("--doctest-modules") |
| 1208 |
reprec.assertoutcome(passed=1) |
| 1209 |
|
| 1210 |
def test_one_skipped_failed(self, pytester, makedoctest): |
| 1211 |
makedoctest( |
| 1212 |
""" |
| 1213 |
>>> 1 + 1 # doctest: +SKIP |
| 1214 |
2 |
| 1215 |
>>> 2 + 2 |
| 1216 |
200 |
| 1217 |
""" |
| 1218 |
) |
| 1219 |
reprec = pytester.inline_run("--doctest-modules") |
| 1220 |
reprec.assertoutcome(failed=1) |
| 1221 |
|
| 1222 |
def test_all_skipped(self, pytester, makedoctest): |
| 1223 |
makedoctest( |
| 1224 |
""" |
| 1225 |
>>> 1 + 1 # doctest: +SKIP |
| 1226 |
2 |
| 1227 |
>>> 2 + 2 # doctest: +SKIP |
| 1228 |
200 |
| 1229 |
""" |
| 1230 |
) |
| 1231 |
reprec = pytester.inline_run("--doctest-modules") |
| 1232 |
reprec.assertoutcome(skipped=1) |
| 1233 |
|
| 1234 |
def test_vacuous_all_skipped(self, pytester, makedoctest): |
| 1235 |
makedoctest("") |
| 1236 |
reprec = pytester.inline_run("--doctest-modules") |
| 1237 |
reprec.assertoutcome(passed=0, skipped=0) |
| 1238 |
|
| 1239 |
def test_continue_on_failure(self, pytester: Pytester): |
| 1240 |
pytester.maketxtfile( |
| 1241 |
test_something=""" |
| 1242 |
>>> i = 5 |
| 1243 |
>>> def foo(): |
| 1244 |
... raise ValueError('error1') |
| 1245 |
>>> foo() |
| 1246 |
>>> i |
| 1247 |
>>> i + 2 |
| 1248 |
7 |
| 1249 |
>>> i + 1 |
| 1250 |
""" |
| 1251 |
) |
| 1252 |
result = pytester.runpytest( |
| 1253 |
"--doctest-modules", "--doctest-continue-on-failure" |
| 1254 |
) |
| 1255 |
result.assert_outcomes(passed=0, failed=1) |
| 1256 |
# The lines that contains the failure are 4, 5, and 8. The first one |
| 1257 |
# is a stack trace and the other two are mismatches. |
| 1258 |
result.stdout.fnmatch_lines( |
| 1259 |
["*4: UnexpectedException*", "*5: DocTestFailure*", "*8: DocTestFailure*"] |
| 1260 |
) |
| 1261 |
|
| 1262 |
def test_skipping_wrapped_test(self, pytester): |
| 1263 |
""" |
| 1264 |
Issue 8796: INTERNALERROR raised when skipping a decorated DocTest |
| 1265 |
through pytest_collection_modifyitems. |
| 1266 |
""" |
| 1267 |
pytester.makeconftest( |
| 1268 |
""" |
| 1269 |
import pytest |
| 1270 |
from _pytest.doctest import DoctestItem |
| 1271 |
|
| 1272 |
def pytest_collection_modifyitems(config, items): |
| 1273 |
skip_marker = pytest.mark.skip() |
| 1274 |
|
| 1275 |
for item in items: |
| 1276 |
if isinstance(item, DoctestItem): |
| 1277 |
item.add_marker(skip_marker) |
| 1278 |
""" |
| 1279 |
) |
| 1280 |
|
| 1281 |
pytester.makepyfile( |
| 1282 |
""" |
| 1283 |
from contextlib import contextmanager |
| 1284 |
|
| 1285 |
@contextmanager |
| 1286 |
def my_config_context(): |
| 1287 |
''' |
| 1288 |
>>> import os |
| 1289 |
''' |
| 1290 |
""" |
| 1291 |
) |
| 1292 |
|
| 1293 |
result = pytester.runpytest("--doctest-modules") |
| 1294 |
assert "INTERNALERROR" not in result.stdout.str() |
| 1295 |
result.assert_outcomes(skipped=1) |
| 1296 |
|
| 1297 |
|
| 1298 |
class TestDoctestAutoUseFixtures: |
| 1299 |
SCOPES = ["module", "session", "class", "function"] |
| 1300 |
|
| 1301 |
def test_doctest_module_session_fixture(self, pytester: Pytester): |
| 1302 |
"""Test that session fixtures are initialized for doctest modules (#768).""" |
| 1303 |
# session fixture which changes some global data, which will |
| 1304 |
# be accessed by doctests in a module |
| 1305 |
pytester.makeconftest( |
| 1306 |
""" |
| 1307 |
import pytest |
| 1308 |
import sys |
| 1309 |
|
| 1310 |
@pytest.fixture(autouse=True, scope='session') |
| 1311 |
def myfixture(): |
| 1312 |
assert not hasattr(sys, 'pytest_session_data') |
| 1313 |
sys.pytest_session_data = 1 |
| 1314 |
yield |
| 1315 |
del sys.pytest_session_data |
| 1316 |
""" |
| 1317 |
) |
| 1318 |
pytester.makepyfile( |
| 1319 |
foo=""" |
| 1320 |
import sys |
| 1321 |
|
| 1322 |
def foo(): |
| 1323 |
''' |
| 1324 |
>>> assert sys.pytest_session_data == 1 |
| 1325 |
''' |
| 1326 |
|
| 1327 |
def bar(): |
| 1328 |
''' |
| 1329 |
>>> assert sys.pytest_session_data == 1 |
| 1330 |
''' |
| 1331 |
""" |
| 1332 |
) |
| 1333 |
result = pytester.runpytest("--doctest-modules") |
| 1334 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 1335 |
|
| 1336 |
def test_doctest_and_python_fixtures_not_shared(self, pytester: Pytester) -> None: |
| 1337 |
"""Fixture scopes are not shared between doctest and python modules. |
| 1338 |
|
| 1339 |
This test is not meant as a hard behavioral test -- sharing scope is |
| 1340 |
also an acceptable behavior (see #14533). But this test ensures and |
| 1341 |
behavior change is done knowingly. |
| 1342 |
""" |
| 1343 |
pytester.makepyfile( |
| 1344 |
r""" |
| 1345 |
import pytest |
| 1346 |
|
| 1347 |
@pytest.fixture(scope="session", autouse=True) |
| 1348 |
def auto(): |
| 1349 |
with open("out", "a", encoding="utf-8") as f: |
| 1350 |
f.write("RUN\n") |
| 1351 |
|
| 1352 |
def test(): |
| 1353 |
pass |
| 1354 |
|
| 1355 |
def func(): |
| 1356 |
'''My function. |
| 1357 |
|
| 1358 |
>>> 1 + 1 |
| 1359 |
2 |
| 1360 |
''' |
| 1361 |
""" |
| 1362 |
) |
| 1363 |
result = pytester.runpytest("--doctest-modules") |
| 1364 |
result.assert_outcomes(passed=2) |
| 1365 |
assert Path("out").read_text("utf-8").split() == ["RUN"] * 2 |
| 1366 |
|
| 1367 |
@pytest.mark.parametrize("scope", SCOPES) |
| 1368 |
@pytest.mark.parametrize("enable_doctest", [True, False]) |
| 1369 |
def test_fixture_scopes(self, pytester, scope, enable_doctest): |
| 1370 |
"""Test that auto-use fixtures work properly with doctest modules. |
| 1371 |
See #1057 and #1100. |
| 1372 |
""" |
| 1373 |
pytester.makeconftest( |
| 1374 |
f""" |
| 1375 |
import pytest |
| 1376 |
|
| 1377 |
@pytest.fixture(autouse=True, scope="{scope}") |
| 1378 |
def auto(request): |
| 1379 |
return 99 |
| 1380 |
""" |
| 1381 |
) |
| 1382 |
pytester.makepyfile( |
| 1383 |
test_1=''' |
| 1384 |
def test_foo(): |
| 1385 |
""" |
| 1386 |
>>> getfixture('auto') + 1 |
| 1387 |
100 |
| 1388 |
""" |
| 1389 |
def test_bar(): |
| 1390 |
assert 1 |
| 1391 |
''' |
| 1392 |
) |
| 1393 |
params = ("--doctest-modules",) if enable_doctest else () |
| 1394 |
passes = 3 if enable_doctest else 2 |
| 1395 |
result = pytester.runpytest(*params) |
| 1396 |
result.stdout.fnmatch_lines([f"*=== {passes} passed in *"]) |
| 1397 |
|
| 1398 |
@pytest.mark.parametrize("scope", SCOPES) |
| 1399 |
@pytest.mark.parametrize("autouse", [True, False]) |
| 1400 |
@pytest.mark.parametrize("use_fixture_in_doctest", [True, False]) |
| 1401 |
def test_fixture_module_doctest_scopes( |
| 1402 |
self, pytester, scope, autouse, use_fixture_in_doctest |
| 1403 |
): |
| 1404 |
"""Test that auto-use fixtures work properly with doctest files. |
| 1405 |
See #1057 and #1100. |
| 1406 |
""" |
| 1407 |
pytester.makeconftest( |
| 1408 |
f""" |
| 1409 |
import pytest |
| 1410 |
|
| 1411 |
@pytest.fixture(autouse={autouse}, scope="{scope}") |
| 1412 |
def auto(request): |
| 1413 |
return 99 |
| 1414 |
""" |
| 1415 |
) |
| 1416 |
if use_fixture_in_doctest: |
| 1417 |
pytester.maketxtfile( |
| 1418 |
test_doc=""" |
| 1419 |
>>> getfixture('auto') |
| 1420 |
99 |
| 1421 |
""" |
| 1422 |
) |
| 1423 |
else: |
| 1424 |
pytester.maketxtfile( |
| 1425 |
test_doc=""" |
| 1426 |
>>> 1 + 1 |
| 1427 |
2 |
| 1428 |
""" |
| 1429 |
) |
| 1430 |
result = pytester.runpytest("--doctest-modules") |
| 1431 |
result.stdout.no_fnmatch_line("*FAILURES*") |
| 1432 |
result.stdout.fnmatch_lines(["*=== 1 passed in *"]) |
| 1433 |
|
| 1434 |
@pytest.mark.parametrize("scope", SCOPES) |
| 1435 |
def test_auto_use_request_attributes(self, pytester, scope): |
| 1436 |
"""Check that all attributes of a request in an autouse fixture |
| 1437 |
behave as expected when requested for a doctest item. |
| 1438 |
""" |
| 1439 |
pytester.makeconftest( |
| 1440 |
f""" |
| 1441 |
import pytest |
| 1442 |
|
| 1443 |
@pytest.fixture(autouse=True, scope="{scope}") |
| 1444 |
def auto(request): |
| 1445 |
if "{scope}" == 'module': |
| 1446 |
assert request.module is None |
| 1447 |
if "{scope}" == 'class': |
| 1448 |
assert request.cls is None |
| 1449 |
if "{scope}" == 'function': |
| 1450 |
assert request.function is None |
| 1451 |
return 99 |
| 1452 |
""" |
| 1453 |
) |
| 1454 |
pytester.maketxtfile( |
| 1455 |
test_doc=""" |
| 1456 |
>>> 1 + 1 |
| 1457 |
2 |
| 1458 |
""" |
| 1459 |
) |
| 1460 |
result = pytester.runpytest("--doctest-modules") |
| 1461 |
str(result.stdout.no_fnmatch_line("*FAILURES*")) |
| 1462 |
result.stdout.fnmatch_lines(["*=== 1 passed in *"]) |
| 1463 |
|
| 1464 |
@pytest.mark.parametrize("scope", [*SCOPES, "package"]) |
| 1465 |
def test_auto_use_defined_in_same_module( |
| 1466 |
self, pytester: Pytester, scope: str |
| 1467 |
) -> None: |
| 1468 |
"""Autouse fixtures defined in the same module as the doctest get picked |
| 1469 |
up properly. |
| 1470 |
|
| 1471 |
Regression test for #11929. |
| 1472 |
""" |
| 1473 |
pytester.makepyfile( |
| 1474 |
f""" |
| 1475 |
import pytest |
| 1476 |
|
| 1477 |
AUTO = "the fixture did not run" |
| 1478 |
|
| 1479 |
@pytest.fixture(autouse=True, scope="{scope}") |
| 1480 |
def auto(request): |
| 1481 |
global AUTO |
| 1482 |
AUTO = "the fixture ran" |
| 1483 |
|
| 1484 |
def my_doctest(): |
| 1485 |
'''My doctest. |
| 1486 |
|
| 1487 |
>>> my_doctest() |
| 1488 |
'the fixture ran' |
| 1489 |
''' |
| 1490 |
return AUTO |
| 1491 |
""" |
| 1492 |
) |
| 1493 |
result = pytester.runpytest("--doctest-modules") |
| 1494 |
result.assert_outcomes(passed=1) |
| 1495 |
|
| 1496 |
|
| 1497 |
class TestDoctestNamespaceFixture: |
| 1498 |
SCOPES = ["module", "session", "class", "function"] |
| 1499 |
|
| 1500 |
@pytest.mark.parametrize("scope", SCOPES) |
| 1501 |
def test_namespace_doctestfile(self, pytester, scope): |
| 1502 |
""" |
| 1503 |
Check that inserting something into the namespace works in a |
| 1504 |
simple text file doctest |
| 1505 |
""" |
| 1506 |
pytester.makeconftest( |
| 1507 |
f""" |
| 1508 |
import pytest |
| 1509 |
import contextlib |
| 1510 |
|
| 1511 |
@pytest.fixture(autouse=True, scope="{scope}") |
| 1512 |
def add_contextlib(doctest_namespace): |
| 1513 |
doctest_namespace['cl'] = contextlib |
| 1514 |
""" |
| 1515 |
) |
| 1516 |
p = pytester.maketxtfile( |
| 1517 |
""" |
| 1518 |
>>> print(cl.__name__) |
| 1519 |
contextlib |
| 1520 |
""" |
| 1521 |
) |
| 1522 |
reprec = pytester.inline_run(p) |
| 1523 |
reprec.assertoutcome(passed=1) |
| 1524 |
|
| 1525 |
@pytest.mark.parametrize("scope", SCOPES) |
| 1526 |
def test_namespace_pyfile(self, pytester, scope): |
| 1527 |
""" |
| 1528 |
Check that inserting something into the namespace works in a |
| 1529 |
simple Python file docstring doctest |
| 1530 |
""" |
| 1531 |
pytester.makeconftest( |
| 1532 |
f""" |
| 1533 |
import pytest |
| 1534 |
import contextlib |
| 1535 |
|
| 1536 |
@pytest.fixture(autouse=True, scope="{scope}") |
| 1537 |
def add_contextlib(doctest_namespace): |
| 1538 |
doctest_namespace['cl'] = contextlib |
| 1539 |
""" |
| 1540 |
) |
| 1541 |
p = pytester.makepyfile( |
| 1542 |
""" |
| 1543 |
def foo(): |
| 1544 |
''' |
| 1545 |
>>> print(cl.__name__) |
| 1546 |
contextlib |
| 1547 |
''' |
| 1548 |
""" |
| 1549 |
) |
| 1550 |
reprec = pytester.inline_run(p, "--doctest-modules") |
| 1551 |
reprec.assertoutcome(passed=1) |
| 1552 |
|
| 1553 |
|
| 1554 |
class TestDoctestReportingOption: |
| 1555 |
def _run_doctest_report(self, pytester, format): |
| 1556 |
pytester.makepyfile( |
| 1557 |
""" |
| 1558 |
def foo(): |
| 1559 |
''' |
| 1560 |
>>> foo() |
| 1561 |
a b |
| 1562 |
0 1 4 |
| 1563 |
1 2 4 |
| 1564 |
2 3 6 |
| 1565 |
''' |
| 1566 |
print(' a b\\n' |
| 1567 |
'0 1 4\\n' |
| 1568 |
'1 2 5\\n' |
| 1569 |
'2 3 6') |
| 1570 |
""" |
| 1571 |
) |
| 1572 |
return pytester.runpytest("--doctest-modules", "--doctest-report", format) |
| 1573 |
|
| 1574 |
@pytest.mark.parametrize("format", ["udiff", "UDIFF", "uDiFf"]) |
| 1575 |
def test_doctest_report_udiff(self, pytester, format): |
| 1576 |
result = self._run_doctest_report(pytester, format) |
| 1577 |
result.stdout.fnmatch_lines( |
| 1578 |
[" 0 1 4", " -1 2 4", " +1 2 5", " 2 3 6"] |
| 1579 |
) |
| 1580 |
|
| 1581 |
def test_doctest_report_cdiff(self, pytester: Pytester): |
| 1582 |
result = self._run_doctest_report(pytester, "cdiff") |
| 1583 |
result.stdout.fnmatch_lines( |
| 1584 |
[ |
| 1585 |
" a b", |
| 1586 |
" 0 1 4", |
| 1587 |
" ! 1 2 4", |
| 1588 |
" 2 3 6", |
| 1589 |
" --- 1,4 ----", |
| 1590 |
" a b", |
| 1591 |
" 0 1 4", |
| 1592 |
" ! 1 2 5", |
| 1593 |
" 2 3 6", |
| 1594 |
] |
| 1595 |
) |
| 1596 |
|
| 1597 |
def test_doctest_report_ndiff(self, pytester: Pytester): |
| 1598 |
result = self._run_doctest_report(pytester, "ndiff") |
| 1599 |
result.stdout.fnmatch_lines( |
| 1600 |
[ |
| 1601 |
" a b", |
| 1602 |
" 0 1 4", |
| 1603 |
" - 1 2 4", |
| 1604 |
" ? ^", |
| 1605 |
" + 1 2 5", |
| 1606 |
" ? ^", |
| 1607 |
" 2 3 6", |
| 1608 |
] |
| 1609 |
) |
| 1610 |
|
| 1611 |
@pytest.mark.parametrize("format", ["none", "only_first_failure"]) |
| 1612 |
def test_doctest_report_none_or_only_first_failure(self, pytester, format): |
| 1613 |
result = self._run_doctest_report(pytester, format) |
| 1614 |
result.stdout.fnmatch_lines( |
| 1615 |
[ |
| 1616 |
"Expected:", |
| 1617 |
" a b", |
| 1618 |
" 0 1 4", |
| 1619 |
" 1 2 4", |
| 1620 |
" 2 3 6", |
| 1621 |
"Got:", |
| 1622 |
" a b", |
| 1623 |
" 0 1 4", |
| 1624 |
" 1 2 5", |
| 1625 |
" 2 3 6", |
| 1626 |
] |
| 1627 |
) |
| 1628 |
|
| 1629 |
def test_doctest_report_invalid(self, pytester: Pytester): |
| 1630 |
result = self._run_doctest_report(pytester, "obviously_invalid_format") |
| 1631 |
result.stderr.fnmatch_lines( |
| 1632 |
[ |
| 1633 |
"*error: argument --doctest-report: invalid choice: 'obviously_invalid_format' (choose from*" |
| 1634 |
] |
| 1635 |
) |
| 1636 |
|
| 1637 |
|
| 1638 |
@pytest.mark.parametrize("mock_module", ["mock", "unittest.mock"]) |
| 1639 |
def test_doctest_mock_objects_dont_recurse_missbehaved(mock_module, pytester: Pytester): |
| 1640 |
pytest.importorskip(mock_module) |
| 1641 |
pytester.makepyfile( |
| 1642 |
f""" |
| 1643 |
from {mock_module} import call |
| 1644 |
class Example(object): |
| 1645 |
''' |
| 1646 |
>>> 1 + 1 |
| 1647 |
2 |
| 1648 |
''' |
| 1649 |
""" |
| 1650 |
) |
| 1651 |
result = pytester.runpytest("--doctest-modules") |
| 1652 |
result.stdout.fnmatch_lines(["* 1 passed *"]) |
| 1653 |
|
| 1654 |
|
| 1655 |
class Broken: |
| 1656 |
def __getattr__(self, _): |
| 1657 |
raise KeyError("This should be an AttributeError") |
| 1658 |
|
| 1659 |
|
| 1660 |
@pytest.mark.parametrize( # pragma: no branch (lambdas are not called) |
| 1661 |
"stop", |
| 1662 |
[ |
| 1663 |
None, |
| 1664 |
pytest.param(_is_mocked, id="is_mocked"), |
| 1665 |
pytest.param(lambda f: None, id="lambda_none"), |
| 1666 |
pytest.param(lambda f: False, id="lambda_false"), |
| 1667 |
pytest.param(lambda f: True, id="lambda_true"), |
| 1668 |
], |
| 1669 |
) |
| 1670 |
def test_warning_on_unwrap_of_broken_object( |
| 1671 |
stop: Callable[[object], object] | None, |
| 1672 |
) -> None: |
| 1673 |
bad_instance = Broken() |
| 1674 |
assert inspect.unwrap.__module__ == "inspect" |
| 1675 |
with _patch_unwrap_mock_aware(): |
| 1676 |
assert inspect.unwrap.__module__ != "inspect" |
| 1677 |
with pytest.warns( |
| 1678 |
pytest.PytestWarning, match="^Got KeyError.* when unwrapping" |
| 1679 |
): |
| 1680 |
with pytest.raises(KeyError): |
| 1681 |
inspect.unwrap(bad_instance, stop=stop) # type: ignore[arg-type] |
| 1682 |
assert inspect.unwrap.__module__ == "inspect" |
| 1683 |
|
| 1684 |
|
| 1685 |
def test_is_setup_py_not_named_setup_py(tmp_path: Path) -> None: |
| 1686 |
not_setup_py = tmp_path.joinpath("not_setup.py") |
| 1687 |
not_setup_py.write_text( |
| 1688 |
'from setuptools import setup; setup(name="foo")', encoding="utf-8" |
| 1689 |
) |
| 1690 |
assert not _is_setup_py(not_setup_py) |
| 1691 |
|
| 1692 |
|
| 1693 |
@pytest.mark.parametrize("mod", ("setuptools", "distutils.core")) |
| 1694 |
def test_is_setup_py_is_a_setup_py(tmp_path: Path, mod: str) -> None: |
| 1695 |
setup_py = tmp_path.joinpath("setup.py") |
| 1696 |
setup_py.write_text(f'from {mod} import setup; setup(name="foo")', "utf-8") |
| 1697 |
assert _is_setup_py(setup_py) |
| 1698 |
|
| 1699 |
|
| 1700 |
@pytest.mark.parametrize("mod", ("setuptools", "distutils.core")) |
| 1701 |
def test_is_setup_py_different_encoding(tmp_path: Path, mod: str) -> None: |
| 1702 |
setup_py = tmp_path.joinpath("setup.py") |
| 1703 |
contents = ( |
| 1704 |
"# -*- coding: cp1252 -*-\n" |
| 1705 |
f'from {mod} import setup; setup(name="foo", description="€")\n' |
| 1706 |
) |
| 1707 |
setup_py.write_bytes(contents.encode("cp1252")) |
| 1708 |
assert _is_setup_py(setup_py) |
| 1709 |
|
| 1710 |
|
| 1711 |
@pytest.mark.parametrize( |
| 1712 |
"name, expected", [("__main__.py", True), ("__init__.py", False)] |
| 1713 |
) |
| 1714 |
def test_is_main_py(tmp_path: Path, name: str, expected: bool) -> None: |
| 1715 |
dunder_main = tmp_path.joinpath(name) |
| 1716 |
assert _is_main_py(dunder_main) == expected |