Python · Software Testing

DOCTEST : An easy testing tool

According to author Daniel Arbuckle, Doctest is the easiest and fastest way to write tests for your python software.Doctest is a program that comes bundled with python that allows you to write down what you expect from your code in a way that’s easy to understand.

Doctests are written in plain text usually in code documentation. The computer reads and runs the tests but ignores the rest of the text that is not part of the test i.e the rest of the documentation. Tests can therefore be embedded in code comments or explanations.

Here is a quick example of a doctest:

def testable(x):
The testable function returns the square root of its parameter, or 3, whichever
is larger.

>>> testable(7)
>>> testable(16)
>>> testable(9)
>>> testable(10) == 10 ** 0.5

if x < 9:
return 3.0
return x ** 0.5

Notice that in the code above the first 13 lines are commented out, the comment simply states how the testable function works and gives examples of expected output when different parameters are passed to it. When running doctest, doctest will read through the comments and try out examples in the comment to see if they return the expected results. This is a great feature especially if you want to check if the documentation is accurate.

More information on doctest can be found here:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s