Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest expected exception

def fib(n):
    if n < 1:
        raise ValueError(f'Invalid parameter {n}')
    a, b = 1, 1
    for _ in range(1, n):
        a, b = b, a+b
    return a

from fibonacci import fib

print(fib(10))
print(fib(-3))

Output:

55
Traceback (most recent call last):
  File "use_fib.py", line 4, in <module>
    print(fib(-1))
  File "fibonacci.py", line 3, in fib
    raise ValueError(f'Invalid parameter {n}')
ValueError: Invalid parameter -1