O'Reilly logo

Test-Driven Python Development by Siddharta Govindaraj

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required