-2

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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    I tried really hard to find a duplicate for this question, but apparently nobody else has ever put the docstring in the wrong place like this before. I guess it's hard to **find out that there is such a thing** as a docstring, without being told immediately how to do it. Anyway, I guess this should be counted as a typo, then. – Karl Knechtel Feb 04 '23 at 09:23

1 Answers1

1

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)
9769953
  • 10,344
  • 3
  • 26
  • 37