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")
When writing tests in an external file, there is no need to put quotes around the contents as we would in a Python file. The entire file content is considered as doctests. Similarly, we also do not need to escape backslashes.
This feature makes it practical to just put all doctests into separate...