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
14
votes
2 answers

How can a python 2 doctest fail and yet have no difference in the values in the failure message?

I'm using Python 2.7.9 in Windows. I have a UTF-8-encoded python script file with the following contents: # coding=utf-8 def test_func(): u""" >>> test_func() u'☃' """ return u'☃' I get a curious failure when I run the…
rakslice
  • 8,742
  • 4
  • 53
  • 57
14
votes
1 answer

How to test exceptions with doctest in Python 2.x and 3.x?

I defined an exception class SpamException in a module spam. Now I want to test a function spam_function, that raises this exception. So I wrote the following doctest. >>> spam_function() Traceback (most recent call last): …
Helmut Grohne
  • 6,578
  • 2
  • 31
  • 67
13
votes
1 answer

Pytesting Doctests in Visual Studio Code Debugger

Suppose I have the following code in foo.py: def start(): """ >>> start() Hello world """ test = 10 print('Hello world') Normally, I would run the doctest by running pytest foo.py --doctest-modules -v in the terminal. I…
Oliver Leung
  • 185
  • 1
  • 6
13
votes
2 answers

How to run (Python-like) doctests in JavaScript?

Do any JavaScript test frameworks provide a rough equivalent to Python's doctest? function add(a, b) { /** Returns the sum of `a` and `b`: > add(1, 3) 4 Add coerces types to numeric values where possible: > add('51' + 3) 54 …
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
12
votes
3 answers

Auto generate doctest output with Sphinx extension

I think I am missing something about the sphinx extension for doctest. The typical example in the documentation is: .. doctest:: >>> print 1 1 Isn't there a way to let sphinx generate the output (here: 1) automatically? As far as I…
user1283990
  • 123
  • 1
  • 4
12
votes
6 answers

C++ equivalent to Python's doctests?

I think the concept of Python's doctests is brilliant, and as a C++ programmer at a real-time shop, I'm quite jealous. We basically have no unit test capability, which is a severe hindrance. I've seen C++Unit, etc, but is there anything that can…
user59513
  • 131
  • 4
11
votes
1 answer

How can I use multi-line input with QuickCheck in doctest?

From Doctest's readme, one can use doctest with QuickCheck, like this: -- | -- prop> sort xs == (sort . sort) (xs :: [Int]) I would like to describe this property using multiple lines, probably like -- | -- prop> sort xs == -- (sort .…
Yosh
  • 2,512
  • 3
  • 24
  • 30
11
votes
6 answers

Line continuation/wrapping in doctest

I am using doctest.testmod() to do some basic testing. I have a function that returns a long string, say get_string(). Something like: def get_string(a, b): r''' (a, b) -> c >>> get_string(1, 2) 'This is \n\n a long \n string with new…
JCOC611
  • 19,111
  • 14
  • 69
  • 90
11
votes
3 answers

Mocking ImportError in Python

I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test…
Attila O.
  • 15,659
  • 11
  • 54
  • 84
11
votes
2 answers

Is there a way to use doctest and sphinx to test and document command line applications?

I have a Python module for which I'm writing a tutorial using Sphinx including doctests. This module comes with a few helper programs. I would like to include those helper programs in the documentation and have doctest check that the standard…
ascobol
  • 7,554
  • 7
  • 49
  • 70
11
votes
2 answers

Doctest failed with zero exit code

In my test code, my doctest fails but the script exits with a zero return value, which causes the CI run to pass, which is not intended. Is this the correct behavior of doctest module? My script ends with: if __name__ == '__main__': import…
Fish Monitor
  • 4,155
  • 3
  • 32
  • 53
11
votes
1 answer

Python: How can I define a class in a doctest?

I would like to use a doctest comment block to demonstrate the usage of a particular base class, but either this cannot be done with doctest or I am doing something wrong. Here is my simple demo code. class MyClass(object): ''' >>> m =…
Sven
  • 985
  • 1
  • 11
  • 27
11
votes
3 answers

Multi version support for Python doctests

I am writing my doctests like this: >>> some_function(a=1, b=2) {u'id': u'123', u'name': u'abc'} This works fine for Python version 2.5, 2.6 & 2.7 but fails for Python 3 with following error: Expected: {u'id': u'123', u'name': u'abc'} Got: …
Irfan
  • 684
  • 5
  • 15
10
votes
2 answers

python doctest: expected result is the same as the "got" result but the test failed

I am on a learning stage of using python as a tool for software QA. I wrote the next simple test in order to find the letter 'a' in a text file number matrix. problem is that the test fails even though the expect equals to what i got. Why is that?…
user734349
  • 101
  • 1
  • 3
10
votes
0 answers

Using Mocks inside Doctests?

I am using doctests. I am wondering what is the correct way to doctest a function that performs an external action (e.g. sends email, connects to a server, etc)? Using Mock seems like the answer but it will muddy up the doc string of the…
m3ta
  • 171
  • 7
1 2
3
29 30