Maintaining doctests

Doctests can be quite verbose, often containing a lot of explanation mixed in with the examples. These doctests can easily run into multiple pages. Sometimes, there could be many lines of doctests followed by just a few lines of code. We can see this happening in our update method. This can make navigating the code more difficult.

We can solve this problem by putting the doctests into a separate file. Suppose, we put the contents of the docstring into a file called readme.txt. We then change our __init__.py file like the following:

if __name__ == "__main__":
    import doctest
    doctest.testfile("readme.txt")

This will now load the contents of readme.txt and run it as doctests.

When writing tests in an external file, there is no need ...

Get Test-Driven Python Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.