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
5
votes
0 answers

Only doctests on immediate module

If I have three modules: -- A.hs module A where -- $ -- >>> True -- True -- B.hs module B where import A -- $ -- >>> True -- True -- C.hs module C where import B -- $ -- >>> True -- True Running doctest C.hs will run all the doctests in all three…
rampion
  • 87,131
  • 49
  • 199
  • 315
5
votes
2 answers

Alternative results in doctests

I have a doctest where I test a float conversion: >>> float('fish') In Python < 2.7 this results in: ValueError: invalid literal for float(): fish In Python 2.7 the result is ValueError: could not convert string to float: fish Can I make both…
pafcu
  • 7,808
  • 12
  • 42
  • 55
5
votes
2 answers

Problems with docstring containing backslash characters

import hashlib def my_function(bytes_): """ >>> my_function(b'\0') '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d' """ return hashlib.sha256(bytes_).hexdigest() if __name__ == "__main__": import…
wim
  • 338,267
  • 99
  • 616
  • 750
5
votes
2 answers

nose-doctest module fixture before module is imported

I use nose for test collection and I also want to use its doctest plugin. I have a module that needs a fixture in order to be importable. Therefore, I cannot use nose's module fixtures, since they are loaded from the module under test. Is there a…
burnpanck
  • 1,955
  • 1
  • 12
  • 36
5
votes
1 answer

python-2.7: doctests ignored in setter method of a class

Why does the following example fail to run its doctest in the setter method? class Foo: a = None @property def a(self): pass @a.setter def a(self, v): ''' >>> 1 == 1 False ''' …
sphakka
  • 457
  • 4
  • 11
5
votes
2 answers

Doctest and Decorators in Python

I was trying to use Python decorator to catch exceptions and log the exceptions. import os.path import shutil class log(object): def __init__(self, f): print "Inside __init__()" self.f = f def __call__(self, *args): …
prosseek
  • 182,215
  • 215
  • 566
  • 871
5
votes
1 answer

prevent sphinx from executing inherited doctests

We've made a library which uses massively (with inheritance) numpy's MaskedArrays. But I want to run sphinx's make doctest without testing the inherited methods from numpy, because they make round about 100 failures. This lookls like this: class…
Themerius
  • 1,861
  • 1
  • 19
  • 33
4
votes
1 answer

Django - Unitest or Doctest?

I'm about to begin my third medium-sized project and would like (for the first time in my life i admit) to start using unittests. I have no idea though, which method to use, unitests or doctests. Which of the methods is the most efficient, or which…
avlnx
  • 676
  • 1
  • 6
  • 19
4
votes
1 answer

Doctests that contain string literals

I have a unit test that I'd like to write for a function that takes XML as a string. It's a doctest and I'd like the XML in-line with the tests. Since the XML is multi-line, I tried a string literal within the doctest, but no success. Here's…
shadowland
  • 757
  • 1
  • 6
  • 20
4
votes
1 answer

@cached_property doctest is not detected

I have a code. a.py from functools import cached_property, cache import doctest class C1: def test_1(self): """ >>> C1().test_1() 'f' """ return "f" @property def test_2(self): """ …
tomlla
  • 519
  • 1
  • 6
  • 15
4
votes
1 answer

Doctest for dynamically created objects

What is the best way to test code like this (the one below obviously fails while object is created in different block every time): def get_session(db_name, verbose, test): """Returns current DB session from SQLAlchemy pool. >>>…
Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62
4
votes
1 answer

pytest log_cli + standard logging = breaks doctest

We're using pytest and python std logging, and have some tests in doctests. We'd like to enable log_cli to make debugging tests in ide easier (lets stderr flow to the "live" console so one can see log statements as they are output when stepping…
some bits flipped
  • 2,592
  • 4
  • 27
  • 42
4
votes
2 answers

How to handle rounding to negative zero in Python docstring tests

Related to this question: How to have negative zero always formatted as positive zero in a python string? I have the following function that implements Matlab's orth.m using Numpy. I have a docstring test that relies on np.array2string using…
DStauffman
  • 3,960
  • 2
  • 21
  • 30
4
votes
1 answer

how to tell PyCharm to run pytest on test files not doctests

In the test suite of a module, some test_foo.py files have helper functions containing doctests patterns in their docstrings. This causes PyCharm to offer Debug 'Doctests in test_foo' when selecting a function inside that file and trying via…
Pierre D
  • 24,012
  • 7
  • 60
  • 96
4
votes
0 answers

Where does doctest move classes that should be in __main__?

I am rather confused about a snippet of code that runs as expected outside of doctest, but seems to fail when run by doctest because a class that exists in __main__ is no longer present. Does doctest do something funny to __main__, and if so where…
Jay Pantone
  • 140
  • 3