By Mad Peach


2020-01-14 13:10:57 8 Comments

Im quite confused why doctest.testmod() is calling the test functions in a strange order

from doctest import testmod

def test_forwrite():
    '''
    >>> test_forwrite()
    OP: Done
    '''
    write()

def test_forread():
    '''
    >>> test_forread()
    OP: Done
    '''
    read()

if __name__ == "__main__":
    testmod(verbose = True)

Why regardless of the order in which its defined test_forread() gets tested first always.

2 comments

@tonyjosi 2020-01-14 13:15:38

Its because the testmod function tests the functions in your given module/program by invoking them in alphabetical (sorted) order.

In your case test_forread() will be invoked first because it comes first before test_forwrite() when sorted in the alphabetical order.

@Martijn Pieters 2020-01-14 13:16:52

Tests are sorted by name. test_forread sorts before test_forwrite(), alphabetically.

From the doctest source code for the DocTestFinder.find() method:

# Sort the tests by alpha order of names, for consistency in
# verbose-mode output.  This was a feature of doctest in Pythons
# <= 2.3 that got lost by accident in 2.4.  It was repaired in
# 2.4.4 and 2.5.
tests.sort()

doctest.testmod() uses DocTestFinder().find() to locate the tests in your module.

Your tests should never rely on any given ordering, however. Write your tests to be independent, that way you could potentially run tests individually or in parallel.

Related Questions

Sponsored Content

4 Answered Questions

Unit testing using doctest in python

  • 2019-02-10 14:55:50
  • Rajeev Dubey
  • 1570 View
  • 0 Score
  • 4 Answer
  • Tags:   python-3.x doctest

18 Answered Questions

[SOLVED] Using global variables in a function

13 Answered Questions

[SOLVED] How to flush output of print function?

18 Answered Questions

[SOLVED] Why is __init__() always called after __new__()?

17 Answered Questions

[SOLVED] How to make a chain of function decorators?

12 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 651633 View
  • 1630 Score
  • 12 Answer
  • Tags:   python object

1 Answered Questions

Python - Testing function without doctest

1 Answered Questions

[SOLVED] pdb is not working in django doctests

2 Answered Questions

[SOLVED] Python doctest execution context

  • 2012-10-03 21:36:34
  • Yanshuai Cao
  • 275 View
  • 1 Score
  • 2 Answer
  • Tags:   python doctest

1 Answered Questions

Web2py Modules and Doctests

Sponsored Content