Doctest is a framework that ships with Python and lets you develop applications using TDD (Test-driven development).
TDD requires writing the tests first and refactoring the code to reach the desired result.
Here doctest will be used, which runs tests based on the documentation written in the code. This means you need clear documentation for each function before developing it, so that the cases where the function works correctly and the cases where it can fail are well defined.
The example code is a function that calculates the square root of a number.
Here is the code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Se importa el módulo math para calcular la raiz cuadrada.
import math
#Función raiz cuadrada.
def RaizCuadrada(a):
"""
>>> RaizCuadrada(4)
2.0
>>> RaizCuadrada(9)
3.0
>>> RaizCuadrada(25)
5.0
>>> RaizCuadrada(0)
0.0
>>> RaizCuadrada(-1)
Traceback (most recent call last):
...
ValueError: a debe ser >= 0
"""
#Si a es mayor o igual a cero se calcula la raiz cuadrada
if a >= 0:
return math.sqrt(a)
#Si es menor a cero se genera una excepción donde se informa que a debe ser mayor o igual a cero.
else:
raise ValueError("a debe ser >= 0")
As shown, the documentation contains a series of execution cases for the function, where it must return a correct value, and if a value that cannot be calculated is passed, it raises the exception.
To run the test, execute the code by calling the doctest module in verbose mode:
python -m doctest -v prueba-raizcuadrada.py
The result is the following:
Trying:
RaizCuadrada(4)
Expecting:
2.0
ok
Trying:
RaizCuadrada(9)
Expecting:
3.0
ok
Trying:
RaizCuadrada(25)
Expecting:
5.0
ok
Trying:
RaizCuadrada(0)
Expecting:
0.0
ok
Trying:
RaizCuadrada(-1)
Expecting:
Traceback (most recent call last):
...
ValueError: a debe ser >= 0
ok
1 items had no tests:
prueba-raizcuadrada
1 items passed all tests:
5 tests in prueba-raizcuadrada.RaizCuadrada
5 tests in 2 items.
5 passed and 0 failed.
Test passed.
As shown, all the cases passed the test. You can check the following documentation and tutorials: The official documentation can be found at the following link.
Another article will explain how to separate the different cases into a text file and keep the program in a separate script.
Make a donation! If you liked the article you can make a donation with Bitcoin (BTC) using the digital wallet of your choice to the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV
Or by scanning the QR code from the wallet:
