Exception raise when use doctest

Viewed 10

Sorry for my bad english. I try to use doctest to verify my function raise some exception. But in fact the exception is correctly raise but my test dont pass despite the same exception is raise on the test. I use python 3.10.4

>>> add_integer = __import__('0-add_integer').add_integer
>>> add_integer(1, 2)
3
>>> add_integer(100, -2)
98
>>> add_integer(2)
100
>>> add_integer(100.3, -2)
98
>>> add_integer(4, "School")
Taceback (most recent call last):
     ...
TypeError: b must be an integer
>>> add_integer(None)
Taceback (most recent call last):
     ...
TypeError: a must be an integer

function to test

#!/usr/bin/python3

"""
Module add_integer define function
to do add operation and handle Type
Error
"""

def add_integer(a, b=98):
    """ add to integer or float
    Args:
    a (int): first number
    b (int): second number (optional)
    """
    if type(a) not in [int, float]:
        raise TypeError("a must be an integer")
    if type(b) not in [int, float]:
        raise TypeError("b must be an integer")
    return int(a) + int(b)

but i still having a bad output

output error I get

0 Answers
Related