Questions tagged [doctest]

The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. The test cases and expected output can be copied from an interactive Python interpreter session. During regression testing doctest alerts about failed cases.

The following overview is from the Python Standard Library documentation doctest page. The examples in the official documentation provide also a nice tutorial to using doctest.

There are several common ways to use doctest:

  • To check that a module’s docstrings are up-to-date by verifying that all interactive examples still work as documented.
  • To perform regression testing by verifying that interactive examples from a test file or a test object work as expected.
  • To write tutorial documentation for a package, liberally illustrated with input-output examples. Depending on whether the examples or the expository text are emphasized, this has the flavor of “literate testing” or “executable documentation”.
446 questions
6
votes
1 answer

python doctest exception test handling

I have the following contents in a file called test2.txt. >>> def faulty(): ... yield 5 ... return 7 Traceback(most recent call last): SyntaxError: 'return' with argument inside generator(,line 3) I invoke the test…
Tracy
  • 1,988
  • 5
  • 25
  • 36
6
votes
1 answer

Python-like doctesting in Java?

One of my favorite features in Python (I know it's not really a feature of python) is doc-testing. For me it really augments standard documentation and helps to keep it up to data. Looking for something similar in Java, I've found JDocTest -…
r0u1i
  • 3,526
  • 6
  • 28
  • 36
6
votes
2 answers

Multi-line string output in Python doctest

I have a Python3 function that returns a multi-line string. I want to test it with doctest, but can't get it to work. I've tried using the +NORMALIZE_WHITESPACE directive without success. def dummy(): """Dummy test function >>> dummy() #…
Justin
  • 1,980
  • 1
  • 16
  • 25
6
votes
0 answers

Run both doctests and normal tests with Pytest

In my Python project, I'm using Pytest. The directory structure is: src/ docs/ test/ I have a different kind of tests: normal tests, in test/* doctests in the source, src/* doctests in documentation, docs/*.rst (Sphinx) I'd like to run all of…
Marco Favorito
  • 390
  • 3
  • 14
6
votes
1 answer

Python: accept unicode strings as regular strings in doctests

Writing doctests for a method that abbreviates a dictionary by searching for a passed key word in the keys of the original dictionary, and returning the new, abbreviated dictionary. My docstring looks as follows: def abbreviate_dict(key_word,…
af3ld
  • 782
  • 8
  • 30
6
votes
1 answer

Doctests fail with UnicodeDecodeError on C-extension and Python3

I am having difficulty getting my testing framework to work for a C-extension module for both Python2 and Python3. I like to run my docstrings through doctest to make sure that I am not feeding my users bad information, so I want to run doctest as…
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
6
votes
1 answer

How to use doctest with a decorated function in python?

I am using a decorator: class Memoized(object): __cache = {} def __init__(self, func): self.func = func key = (func.__module__, func.__name__) # print key if key not in self.__cache: …
Farshid Ashouri
  • 16,143
  • 7
  • 52
  • 66
6
votes
4 answers

Examples of using Doctests in Django in an Agile / BDD way

I'm interested in learning how to Doctests and Unit tests in a more Agile / BDD way. I've found a few tutorials that seem reasonable, but they are just thumbnails. What I would really like to see is the source code of some Django projects that were…
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109
5
votes
3 answers

How to terminate a python 2 doctest file in the middle?

Sometimes it can be useful to run only the first part of a large doctests file. There are many situations when the first part breaks after a code change, I would like to run only the first part, until it passes, and then run the whole file again. I…
mit
  • 11,083
  • 11
  • 50
  • 74
5
votes
1 answer

Doctest of function with random output

I have a function that prints a somewhat random string to the console, for example: from random import choice def hello(): print(f"Hello {choice(('Guido', 'Raymond'))}!") Please note that my actual function is more complicated than this. The…
edd313
  • 1,109
  • 7
  • 20
5
votes
0 answers

Combining doctest and Matplotlib Sphinx extensions

The Sphinx extension sphinx.ext.doctest makes it straightforward to ensure that your documentation is up to date. At the same time, the matplotlib.sphinxext.plot_directive extension makes it straightforward to auto-generate figures as part of your…
fuglede
  • 17,388
  • 2
  • 54
  • 99
5
votes
1 answer

Include raw tab literal character in doctest

I can't figure out how to avoid this doctest error: Failed example: print(test()) Expected: output Got: output For this code def test(): r'''Produce string according to specification. >>>…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
5
votes
2 answers

Where to put implementation when using Doctest alongside code

I'm using doctest for the tests in my C++ project. I would like to put the test code alongside my implementations, as the library says is possible, but I can't seem to figure out what to do with the doctest implementation code. I have a doctest.cpp…
Omegastick
  • 1,773
  • 1
  • 20
  • 35
5
votes
4 answers

Running tests against code examples in a README.md?

Does anyone know of an open-source project or program for running tests against code examples in README.md? A perennial problem, my documentation tends to drift out of date with the code. For example, a code snippet in the README.md will no longer…
rmharrison
  • 4,730
  • 2
  • 20
  • 35
5
votes
1 answer

How to run all doctests in a folder recursively in pycharm?

Pycharm is great for running all of the doctests per (script / function / class / folder*) but the folder option runs all doctests which are immediately within that folder (not within folders of that folder). Is there a way to recursively run…
eretmochelys
  • 543
  • 1
  • 5
  • 15