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

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

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

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


@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.

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

13 Answered Questions

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

  • 2008-08-06 03:36:08
  • ricree
  • 718327 View
  • 1758 Score
  • 13 Answer
  • Tags:   python object

19 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__()?

5 Answered Questions

[SOLVED] Unit testing using doctest in python

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

17 Answered Questions

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

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
  • 304 View
  • 1 Score
  • 2 Answer
  • Tags:   python doctest

1 Answered Questions

Web2py Modules and Doctests

Sponsored Content