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
8
votes
4 answers

How to use doctest with logging?

The following doctest fails: import logging logging.basicConfig(level=logging.DEBUG,format='%(message)s') def say_hello(): ''' >>> say_hello() Hello! ''' logging.info('Hello!') if __name__ == '__main__': import doctest …
Ali
  • 56,466
  • 29
  • 168
  • 265
8
votes
2 answers

Can I unit test an inner function in python?

Is there any way to write unittests or doctests for innerfunc? def outerfunc(): def innerfunc(): do_something() return innerfunc()
Gattster
  • 4,613
  • 5
  • 27
  • 39
8
votes
3 answers

object reuse in python doctest

I have a sample doctest like this one. """ This is the "iniFileGenerator" module. >>> hintFile = "./tests/unit_test_files/hint.txt" >>> f = iniFileGenerator(hintFile) >>> print f.hintFilePath ./tests/unit_test_files/hint.txt """ class…
prosseek
  • 182,215
  • 215
  • 566
  • 871
8
votes
2 answers

Doctest Involving Escape Characters

Have a function fix(), as a helper function to an output function which writes strings to a text file. def fix(line): """ returns the corrected line, with all apostrophes prefixed by an escape character >>> fix('DOUG\'S') …
zhuyxn
  • 6,671
  • 9
  • 38
  • 44
7
votes
1 answer

Doctest for nested docstring

Suppose I have following code: def foo(s): """A dummy function foo. For example: >>> a = '''This is a test string line 1 This is a test string line 2 This is a test string line 3''' >>> foo(a) This is a test string line 1 This is a test string…
user1045217
  • 506
  • 1
  • 4
  • 9
7
votes
2 answers

python doctest default namespace

In the doctests of my module I would like to reference my module with the full namespace, for example: hp.myfunc(1) And I would like to avoid cluttering the doctests by writing: import healpy as hp in each of the doctests. if I run…
Andrea Zonca
  • 8,378
  • 9
  • 42
  • 70
7
votes
2 answers

How do you test a function using which retrieves data by urllib2?

I am getting into testing in python and I asked myself how to test this method. def get_response(self, url, params): encoded_params = urllib.urlencode(params) request = urllib2.Request(BASE_URL, headers=HEADERS) response =…
Aufwind
  • 25,310
  • 38
  • 109
  • 154
7
votes
1 answer

How do I test async functions with doctest?

Example code: async def test(): """ >>> await test() hello world """ print("hello world") Attempting to run this with doctests results in SyntaxError: 'await' outside function.
Bharel
  • 23,672
  • 5
  • 40
  • 80
7
votes
3 answers

How to let pytest discover and run doctests in installed modules?

I am adding unit tests and to a kind of "legacy" Python package. Some of the modules contain their own doctests embedded in docstrings. My goal is to run both those doctests and new, dedicated unit tests. Following this Q&A ("How to make py.test run…
Cong Ma
  • 10,692
  • 3
  • 31
  • 47
7
votes
3 answers

use doctest and logging in python program

#!/usr/bin/python2.4 import logging import sys import doctest def foo(x): """ >>> foo (0) 0 """ print ("%d" %(x)) _logger.debug("%d" %(x)) def _test(): doctest.testmod() _logger =…
Luke
  • 148
  • 4
7
votes
1 answer

Why does nose finds tests in files with only 644 permission?

Today I ran a bunch of doctests using Python 2.6 on a Ubuntu 9.10 with nose : nosetests --with-doctest Ran 0 tests in 0.001s OK WTF? I had tests in that files, why didn't that work? I changed permission to 644: sudo chmod 644 * -R nosetests…
Bite code
  • 578,959
  • 113
  • 301
  • 329
7
votes
1 answer

Run Django doctests from PyCharm

PyCharm's test runner does a great job with unit tests, likewise the run command contextually recognizes if you are in a unit test in a helpful way. Is there any way to get it to do the same with doctests running within the Django environment? (As…
Ghopper21
  • 10,287
  • 10
  • 63
  • 92
6
votes
2 answers

How to work with nulls in docutils

I'm trying to run doctest on a function that works with nulls. But doctest doesn't seem to like the nulls... def do_something_with_hex(c): """ >>> do_something_with_hex('\x00') '\x00' """ return repr(c) import…
Mark Irvine
  • 1,349
  • 14
  • 24
6
votes
4 answers

How to determine whether code is running in a doctest?

How can I make my (Python 2.7) code aware whether it is running in a doctest? The scenario is as follows: I have a function that print()s some output to a file descriptor passed in as an argument, something like this: from __future__ import…
mshroyer
  • 1,928
  • 17
  • 14
6
votes
3 answers

django doctests not being run

I'm having a problem running django doctests with django-nose. Unit tests added to a /tests directory are running fine, but doctests are not. I am trying to run doctests on my "season" module: python manage.py test season and get this…
rrw4
  • 61
  • 2