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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.