Register to get access to free programming courses with interactive exercises

Testing through documentation Python: Automated testing

Python has built-in testing through documentation in addition to the classic tests. It is an unusual way to write tests, but developers sometimes use it for library functions. In this lesson, we'll study this topic in more detail to learn what such documentation looks like.

Testing through documentation

The idea is pretty simple. We call a function inside the terminal. The call itself and the resulting output are the tests. Then, we add them to the documentation of the function:

# Example.py has a function called `reverse()` that reverses a string
# The `-i` flag turns on the interactive mode after module execution
python -i example.py
reverse('')
# ''
reverse('Hexlet')
# 'telxeH'

After making the necessary calls, we add them to the function description:

def reverse(string):
    """Reverse string

    >>> reverse('')
    ''

    >>> reverse('Hexlet')
    'telxeH'
    """

    return string[::-1]

# We need to run tests
if __name__ == "__main__":
    import doctest
    doctest.testmod()

If we pass this file to the interpreter, we'll see a report on the tests performed:

# For the detailed output, you need to add a flag `-v` (verbose)
python example.py -v
Trying:
    reverse('')
Expecting:
    ''
ok
Trying:
    reverse('Hexlet')
Expecting:
    'telxeH'
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.reverse
2 tests in 2 items.
2 passed and 0 failed.
Test passed.

It is how documentation can simultaneously act as tests, which is convenient and practical. We can do the same not only in a specific function but also at the module level:

"""
It is the example module.

The example module supplies the `reverse()` function, for example:

>>> reverse('awesome!')
'!emosewa'
"""

Recommended materials

  1. Doctest documentation

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.