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”.
399 questions
164
votes
11 answers

Python - doctest vs. unittest

I'm trying to get started with unit testing in Python and I was wondering if someone could explain the advantages and disadvantages of doctest and unittest. What conditions would you use each for?
Sean
  • 4,994
  • 6
  • 26
  • 27
78
votes
3 answers

Custom PyCharm docstring stubs (i.e. for google docstring or numpydoc formats)

Does PyCharm 2.7 (or will PyCharm 3) have support for custom docstring and doctest stubs? If so, how does one go about writing this specific type of custom extension? My current project has standardized on using the Google Python Style Guide…
kalefranz
  • 3,948
  • 2
  • 24
  • 41
65
votes
3 answers

Can you check that an exception is thrown with doctest in Python?

Is it possible to write a doctest unit test that will check that an exception is raised? For example, if I have a function foo(x) that is supposed to raise an exception if x<0, how would I write the doctest for that?
Lorin Hochstein
  • 51,296
  • 30
  • 96
  • 131
45
votes
4 answers

Python: using doctests for classes

Is it possible to use Python's doctest concept for classes, not just functions? If so, where shall I put the doctests - at the class' docstring, or at the constructor's docstring? To clarify, I'm looking for something like: class Test: """ …
Adam Matan
  • 107,447
  • 124
  • 346
  • 512
42
votes
7 answers

How do I test dictionary-equality with Python's doctest-package?

I'm writing a doctest for a function that outputs a dictionary. The doctest looks like >>> my_function() {'this': 'is', 'a': 'dictionary'} When I run it, it fails with Expected: {'this': 'is', 'a': 'dictionary'} Got: {'a': 'dictionary',…
Emmett Butler
  • 5,099
  • 2
  • 27
  • 44
42
votes
2 answers

Wrapping python doctest results that are longer than 80 characters

I'm trying to keep my source code under the 80 character guideline width that PEP8 recommends, but can't figure out how to wrap my doctest which has results longer than 80 characters. A noddy example: def long_string(): """ Returns a string…
pelson
  • 18,423
  • 3
  • 80
  • 88
37
votes
6 answers

Can python doctest ignore some output lines?

I'd like to write a doctest like this: """ >>> print a.string() foo : a bar : b date : baz : c """ Is there any way to do this? I think it would make more sense to switch…
cjb
  • 679
  • 1
  • 6
  • 8
30
votes
3 answers

How do I run doctests with PyCharm?

In the PyCharm IDE, if I right-click on a function/method with a doctest, sometimes the right-click menu will give me the option: "Run 'Doctest my_function_name'" and sometimes the right-click menu, instead, only gives the option to run the whole…
Troy
  • 19,207
  • 19
  • 68
  • 96
29
votes
3 answers

Python doctests: test for None

Using Python 2.7 I'm trying to test that the result of a particular function call is None I would expect these tests to pass (excuse the rather silly example) def six_or_none(val): """ >>> six_or_none(6) 6 >>> six_or_none(4) …
robert_b_clarke
  • 1,343
  • 9
  • 13
28
votes
3 answers

Python doctest with newline characters: inconsistent leading whitespace error

When writing python doctests, how does one properly introduce newline characters within a string in the test? Here's a simple example: def remove_newlines(text): """ >>> remove_newlines("line1 \n" ... "still line 1\r" …
kalefranz
  • 3,948
  • 2
  • 24
  • 41
24
votes
7 answers

Django doctests in views.py

The Django 1.4 documentation on tests states: For a given Django application, the test runner looks for doctests in two places: The models.py file. You can define module-level doctests and/or a doctest for individual models. It's common practice…
Brian M. Hunt
  • 71,376
  • 65
  • 208
  • 328
23
votes
4 answers

How to make pytest run doctests as well as normal tests directory?

We currently have pytest with the coverage plugin running over our tests in a tests directory. What's the simplest way to also run doctests extracted from our main code? --doctest-modules doesn't work (probably since it just runs doctests from…
Yang
  • 15,052
  • 13
  • 96
  • 137
23
votes
1 answer

Is it possible to only test specific functions with doctest in a module?

I am trying to get into testing in Python using the doctest module. At the moment I do Write the tests for the functions. implement the functions code. If Tests pass, write more tests and more code. When the function is done move on to the next…
Aufwind
  • 22,034
  • 33
  • 94
  • 149
21
votes
2 answers

Doctest and relative imports

I'm having trouble using doctest with relative imports. The simple solution is just to get rid of the relative imports. Are there any others? Say I have a package called example containing 2 files: example/__init__.py """ This package is entirely…
Ben Reynwar
  • 1,309
  • 11
  • 18
20
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
  • 21,564
  • 16
  • 57
  • 65
1
2 3
26 27