doctest#
Última modificación: Mayo 14, 2022
[1]:
%%writefile example.py
"""
This is the "example" module.
The example module supplies one function, factorial(). For example,
>>> factorial(5)
120
"""
def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000
It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
"""
import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
result *= factor
factor += 1
return result
if __name__ == "__main__":
import doctest
doctest.testmod()
Overwriting example.py
[2]:
#
# Ejecución
#
!python3 example.py
[3]:
#
# verbose
#
!python3 example.py -v
Trying:
factorial(5)
Expecting:
120
ok
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
factorial(30)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(-1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be >= 0
ok
Trying:
factorial(30.1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be exact integer
ok
Trying:
factorial(30.0)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(1e100)
Expecting:
Traceback (most recent call last):
...
OverflowError: n too large
ok
2 items passed all tests:
1 tests in __main__
6 tests in __main__.factorial
7 tests in 2 items.
7 passed and 0 failed.
Test passed.
Verificación de ejemplos en Docstrings#
Llamado a DocTest dentro del código
if __name__ == "__main__":
import doctest
doctest.testmod()
Llamado a DocTest por fuera del código
$ python3 -m doctest -v example.py
Verificación de ejemplos en un archivo de texto#
[4]:
%%writefile example.txt
The ``example`` module
======================
Using ``factorial``
-------------------
This is an example text file in reStructuredText format. First import
``factorial`` from the ``example`` module:
>>> from example import factorial
Now use it:
>>> factorial(6)
120
Overwriting example.txt
[5]:
%%writefile test.py
import doctest
doctest.testfile("./example.txt")
Overwriting test.py
[6]:
!python3 test.py
**********************************************************************
File "./example.txt", line 14, in example.txt
Failed example:
factorial(6)
Expected:
120
Got:
720
**********************************************************************
1 items had failures:
1 of 2 in example.txt
***Test Failed*** 1 failures.
[7]:
!python3 -m doctest -v example.txt
Trying:
from example import factorial
Expecting nothing
ok
Trying:
factorial(6)
Expecting:
120
**********************************************************************
File "example.txt", line 14, in example.txt
Failed example:
factorial(6)
Expected:
120
Got:
720
**********************************************************************
1 items had failures:
1 of 2 in example.txt
2 tests in 1 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.