Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion run_doctests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ def process_options(
# Implement some handy defaults
if not paths_are_modules:
if not "module_relative" in opts_dict:
opts_dict["module_relative"] = False # we want this OFF, by default anyway
opts_dict["module_relative"] = (
False # we want this OFF, by default anyway
)
if not "optionflags" in opts_dict:
default_flags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
opts_dict["optionflags"] = default_flags # a generally useful default?
Expand Down Expand Up @@ -299,6 +301,12 @@ def run_doctest_paths(

if __name__ == "__main__":
args = _parser.parse_args(sys.argv[1:])
# Empty sys.argv after parsing, as doctest functions check it even when
# called directly :-(.
# This is principally to prevent "-v" forcing verbose output from doctests,
# which "should" only occur with "-o verbose=True".
sys.argv = sys.argv[:1]

if not args.paths:
_parser.print_help()
else:
Expand Down
86 changes: 86 additions & 0 deletions tests/test_run_doctests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ def badsources(tempsources):
return tempsources


@pytest.fixture
def mixedsources(tmp_path):
# Like tempsources/badsources, but provide 2 files with a misture of good + bad
patt = {
"goodbads": ["s0.rst", "s1.rst"],
}
make_dirs_and_files(patt, tmp_path)
with open(tmp_path / "goodbads/s0.rst", "wt") as f_out:
f_out.write(">>> 123\n123\n") # OK test
f_out.write("\n\n")
f_out.write(">>> 1\n2\n") # FAILING test
f_out.write("\n\n")
with open(tmp_path / "goodbads/s1.rst", "wt") as f_out:
f_out.write(">>> print(4)\n4\n") # OK test (only)
return str(tmp_path / "goodbads") # targets must be str, not Path


class TestListModules:
def test_import(self, tempmodules):
"""Check that 'tempmodules' puts the test module on the import path."""
Expand Down Expand Up @@ -202,6 +219,7 @@ def test_recurse_match_nonexist_noglob(self, tempsources, tmp_path):


def runmain(*args, expect_rc: int | None = 0) -> list[str]:
"""Run with args and return a list of output strings."""
arglist = [_PYTHON_PATHSTR, _DOCTESTS_PATH] + list(args)
call_data = subprocess.run(arglist, capture_output=True)
rc = call_data.returncode
Expand Down Expand Up @@ -351,6 +369,74 @@ def test_multi_exclude(self, tempsources):
]
]

@pytest.mark.parametrize("verbose", [False, True], ids=["noisy", "quiet"])
def test_verbose(self, mixedsources, verbose):
args = [mixedsources + "/**/*.rst"]
if verbose:
args.append("-v")
result_lines = runmain(*args, expect_rc=1)
# Handle a small change to doctests between Python 3.12 and 3.13:
# used to say e.g. "1 failures, 2 failures" -- now "1 failure, 2 failures"
result_lines = [
line.replace("failures", "failure").replace("items", "item")
for line in result_lines
]
expect_lines = []
if verbose:
# preamble -- only when verbose
expect_lines.extend(
[
"RUNNING "
f"run_doctest(paths=['{mixedsources}/**/*.rst'], "
"paths_are_modules=False, recurse_modules=False, "
"exclude_fragments=[], "
"doctest_kwargs={'module_relative': False, 'optionflags': 12}, "
"verbose=True, dry_run=False, stop_on_failure=False)",
"-----",
f"doctest.testfile: {mixedsources}/s0.rst",
]
)
# report on first file (with error) -- full including error output
expect_lines.extend(
[
"**********************************************************************",
f'File "{mixedsources}/s0.rst", line 5, in s0.rst',
"Failed example:",
" 1",
"Expected:",
" 2",
"Got:",
" 1",
"**********************************************************************",
"1 item had failure:",
" 1 of 2 in s0.rst",
"***Test Failed*** 1 failure.",
"**ERRORS**",
f"1/2 OK, 1/2 FAILED in path: {mixedsources}/s0.rst",
]
)
if verbose:
# report on second file (no errors) -- only when verbose
expect_lines.extend(
[
"-----",
f"doctest.testfile: {mixedsources}/s1.rst",
f"1/1 OK in path: {mixedsources}/s1.rst",
]
)

expect_lines.extend(
[
"=====",
"run_doctest: FINAL REPORT",
" paths tested = 2",
" tests completed = 3",
" errors = 1",
"FAILED.",
]
)
assert result_lines == expect_lines


class TestCliModules:
@pytest.mark.parametrize("recurse", [False, True])
Expand Down
Loading