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
10
votes
7 answers

Doctesting functions that receive and display user input - Python (tearing my hair out)

I am currently writing a small application with Python (3.1), and like a good little boy, I am doctesting as I go. However, I've come across a method that I can't seem to doctest. It contains an input(), an because of that, I'm not entirely sure…
GlenCrawford
  • 3,359
  • 3
  • 26
  • 34
10
votes
2 answers

Running doctests from Pydev?

Is there any straightforward way or should I use an external tool like Nose?
yanchenko
  • 56,576
  • 33
  • 147
  • 165
10
votes
1 answer

Python doctest exceptions

So, I'm trying to match an exception with a doctest. >>> api = Api("foo", "bar") # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... AuthError The issue is that this works with py2.7 but not with python 3. The format of…
Nikolay Derkach
  • 1,734
  • 2
  • 22
  • 34
10
votes
5 answers

Configure Django to find all doctests in all modules?

If I run the following command: >python manage.py test Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from…
Chase Seibert
  • 15,703
  • 8
  • 51
  • 58
9
votes
2 answers

ValueError: wrapper loop when unwrapping

Python3 test cases (doctests) are failing with my sample code. But the same is working fine in Python2. test.py class Test(object): def __init__(self, a=0): self.a = a def __getattr__(self, attr): return Test(a=str(self.a) +…
Ram Idavalapati
  • 666
  • 1
  • 10
  • 22
9
votes
3 answers

How to write Python doctest that is OS independent regarding path separator

Is there a way to have a doctest with file paths as output that will succeed regardless of the OS it's run on? For example, on Windows this will work: r""" >>> import foo >>> relative_path = foo.getRelativePath() >>> print relative_path…
asherbret
  • 5,439
  • 4
  • 38
  • 58
9
votes
2 answers

doctest locally defined functions

is there any way to doctest locally defined functions? As an example I would want def foo(): """ >>> foo() testfoo""" def foo2(): """ >>> 1/0 """ print 'testfoo' foo2() to NOT pass the test. But still I would not want to make…
9
votes
1 answer

Are portable doctests for python 2 / python 3 possible?

def fib_r(n, memo={0: 0, 1: 1}): """recursive fibonacci numbers generation with memoisation >>> [fib_r(n) for n in range(10)] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] >>> fib_r(100) 354224848179261915075""" if n not in memo: …
wim
  • 338,267
  • 99
  • 616
  • 750
9
votes
1 answer

Doctest NORMALIZE_WHITESPACE does not work

Failed example: p.parse_name('Adams, Michael') # doctest: +NORMALIZE_WHITESPACE Expected: {'first_name': 'Michael', 'last_name': 'Adams','initials': 'MA'} Got: {'first_name': 'Michael', 'last_name': 'Adams', 'initials': 'MA'} The…
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
8
votes
3 answers

Python doctest for shell scripts that test argument parsing without polluting docstring with os.popen()

Is there a way to write a python doctest string to test a script intended to be launched from the command line (terminal) that doesn't pollute the documentation examples with os.popen calls? #!/usr/bin/env python # filename: add """ Example: >>>…
hobs
  • 18,473
  • 10
  • 83
  • 106
8
votes
2 answers

CMake: prevent building test executable target in subdirectory library project

I have written a small library that uses doctest. In CMakeLists.txt I have: ... add_library(my_lib STATIC ${SRCS} $) add_executable(tests ${SRCS} $) target_compile_definitions(my_lib PRIVATE…
PaulR
  • 706
  • 9
  • 27
8
votes
2 answers

Run all my doctests for all python modules in a folder without seeing failures because of bad imports

I've started integrating doctests into my modules. (Hooray!) These tend to be files which started as scripts, and are now are a few functions with CLI apps in the __name__=='__main__', so I don't want to put the running of the tests there. I…
Thomas
  • 6,515
  • 1
  • 31
  • 47
8
votes
2 answers

Doctest not recognizing __future__.division

I have the following doctest written x.doctest: This is something: >>> x = 3 + 4 foo bar something else: >>> from __future__ import division >>> y = 15 >>> z = int('24') >>> m = z / y >>> print (m) 1.6 But when I ran…
alvas
  • 115,346
  • 109
  • 446
  • 738
8
votes
6 answers

Unit testing infrastructure for a python module

I'm writing a python module and I would like to unit test it. I am new to python and somewhat bamboozled by the options available. Currently, I would like to write my tests as doctests as I like the declarative rather than imperative style…
fmark
  • 57,259
  • 27
  • 100
  • 107
8
votes
3 answers

AttributeError: 'module' object has no attribute 'testmod' Python doctest

When ever I try to doctest in python, basically whenever I run the code if __name__ =="__main__": import doctest doctest.testmod() I get this response from the interpreter AttributeError: 'module' object has no attribute 'testmod' I can run…
Johnny Mann
  • 81
  • 1
  • 3