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
22
votes
1 answer

How can I integrate doctests with unittest's test discovery?

I wrote a python script to do all my tests automatically for me, and generate a HTML report. I discovered discover for unittests the other day which lets me run all the unittests in a given directory without explicitly naming them, and I'd really…
bluprince13
  • 4,607
  • 12
  • 44
  • 91
22
votes
6 answers

Python doctest: Skip entire block?

I've got a Python module with docstrings in class methods, and a real-world example in the module docstring. The distinction is that the method-docstrings have been carefully crafted to be utterly repeatable tests, while the real-world example is…
RobM
  • 8,373
  • 3
  • 45
  • 37
21
votes
6 answers

How can I include special characters (tab, newline) in a python doctest result string?

Given the following python script: # dedupe.py import re def dedupe_whitespace(s,spacechars='\t '): """Merge repeated whitespace characters. Example: >>> dedupe_whitespace(r"Green\t\tGround") # doctest: +REPORT_NDIFF …
hobs
  • 18,473
  • 10
  • 83
  • 106
21
votes
2 answers

Multiline statements using Python doctest

Is it possible to work with multiline statements using python doctest? For example, the following is not working in doctest: >>> for s in [1,2,3]: ... for t in [4,5,6]: ... print(s*t) I need the above three statements to be executed…
Rajesh Kumar
  • 1,270
  • 4
  • 15
  • 31
21
votes
5 answers

How to test floats results with doctest?

I'm developing a program that makes some floating points calculations. Is there any way to test my functions (which deliver floats) with doctests?
malev
  • 235
  • 2
  • 5
21
votes
5 answers

How do I include unicode strings in Python doctests?

I am working on some code that has to manipulate unicode strings. I am trying to write doctests for it, but am having trouble. The following is a minimal example that illustrates the problem: # -*- coding: utf-8 -*- def mylen(word): """ >>>…
saffsd
  • 23,742
  • 18
  • 63
  • 67
20
votes
4 answers

Possible to run python doctest on a jupyter cell function?

There seems to be a package to enable this functionality, but I have no luck with it in python 3.5.2, or 2.7.12: from ipython_doctester import test @test def my_fun(): ''' >>> 2 + 3 6 ''' pass TypeError: data must be a dict,…
Rich L
  • 369
  • 3
  • 12
17
votes
5 answers

Can I have an ellipsis at the beginning of the line in a Python doctest?

Python doctests are cool. Let me start with a simple example: def foo(): """ >>> foo() hello world """ print "hello world" Now let's assume some part is somewhat varying, e.g., because it is a time value or a random number. In general,…
BjoernD
  • 4,720
  • 27
  • 32
17
votes
2 answers

Python doctest: skip a test conditionally

I know how to skip a doctest using # doctest: +SKIP, but I can't figure out how to skip a test sometimes, based on a runtime condition. For example: >>> if os.path.isfile("foo"): ... open("foo").readlines() ... else: ... pass # doctest:…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
17
votes
2 answers

Can I make the pytest doctest module ignore a file?

We use pytest to test our project and have enabled --doctest-modules by default to collect all of our doctests from across the project. However there is one wsgi.py which may not be imported during test collection, but I cant get pytest to ignore…
NiklasMM
  • 2,895
  • 2
  • 22
  • 26
15
votes
3 answers

Python doctest: result with multiple lines

I can't get a doctest to work with a result which contains multiple lines and may contain empty lines at the beginning. This is probably caused by indentation and parsing issues. I've figured out some solutions: Write the desired result to a file,…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
15
votes
6 answers

Does Python doctest remove the need for unit-tests?

A fellow developer on a project I am on believes that doctests are as good as unit-tests, and that if a piece of code is doctested, it does not need to be unit-tested. I do not believe this to be the case. Can anyone provide some solid, ideally…
daniel
  • 2,568
  • 24
  • 32
15
votes
7 answers

Testing warnings with doctest

I'd like to use doctests to test the presence of certain warnings. For example, suppose I have the following module: from warnings import warn class Foo(object): """ Instantiating Foo always gives a warning: >>> foo = Foo() …
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
15
votes
4 answers

python: doctest my github-markdown files?

I'd like to run the doctests from this file, and it's not clear to me out to accomplish it: README.md: # WELCOME! This library is helpful and will help you in _many_ ways! For example: ``` >>> import library >>> library.helps() True ``` (aside:…
bukzor
  • 37,539
  • 11
  • 77
  • 111
14
votes
3 answers

Is there a way to restart or reset the python interpreter within a python doctest?

I am writing a short tutorial, and would like to be able to run the examples therein using python's doctest using python -m doctest foo.txt There is a point in the tutorial at which I want to start using a new, clean python interpreter. Is there a…
EHN
  • 598
  • 1
  • 5
  • 13
1
2
3
29 30