Search code examples
pythondoctest

Module doctest does not run


I am trying to use the doctest module to test code. I tried this example:

import doctest 

def areaTriangulo(base, altura):
    return 'El area del triangulo es: '+str((base*altura)/2)
    """
    funcion que nos devuelve el area de un triangulo
    >>> areaTriangulo(4,5)
    'El area del triangulo es: 20.0'
    """

doctest.testmod()

The test has a wrong answer on purpose, but the test tells me that there are no mistakes. Why?


Solution

  • Make sure the docstring is at the top of the function definition; not at the bottom; otherwise Python won't recognise it as a docstring:

    def areaTriangulo(base, altura):
        """
            
        funcion que nos devuelve el area de un triangulo
        
        >>> areaTriangulo(4,5)
        'El area del triangulo es: 20.0'
            
        """
        
        return 'El area del triangulo es: '+str((base*altura)/2)