Exemples du cours¶
In [1]:
Copied!
def fibo(n: int) -> int:
"""
fibo(n) : n-ième terme de la suite de Fibonacci
>>> fibo(3)
2
>>> fibo(7)
14
"""
if n< 0:
raise ValueError("La valeur ne doit pas être négative")
# First Fibonacci number is 0
elif n == 0:
return 0
# Second Fibonacci number is 1
elif n == 1:
return 1
else:
return fibo(n-1)+fibo(n-2)
def fibo(n: int) -> int:
"""
fibo(n) : n-ième terme de la suite de Fibonacci
>>> fibo(3)
2
>>> fibo(7)
14
"""
if n< 0:
raise ValueError("La valeur ne doit pas être négative")
# First Fibonacci number is 0
elif n == 0:
return 0
# Second Fibonacci number is 1
elif n == 1:
return 1
else:
return fibo(n-1)+fibo(n-2)
Utilisation du module pytest¶
# Fichier test_Fibo.py
import pytest
def test_0():
assert fibo(0) == 0
def test_3():
assert fibo(3) == 2
def test_7():
assert fibo(7) == 13
def test_neg():
with pytest.raises(ValueError):
fibo(-1)
pytest.main()
Résultat du module pytest¶
============================= test session starts =============================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\Desktop
plugins: anyio-2.2.0
collected 4 items
test_Fibo.py .... [100%]
============================== 4 passed in 1.08s ==============================
Reloaded modules: test_Fibo
============================= test session starts =============================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\Desktop
plugins: anyio-2.2.0
collected 4 items
test_Fibo.py .... [100%]
============================== 4 passed in 2.15s ==============================