Introduction aux tests en Python
Alexander Levin
Data Scientist
Rappelez-vous l'analogie du « pique-nique » :
Il est important de nettoyer l'environnement à la fin d'un test. Sans démontage, on risque :
Quand l'utiliser :
autouseQuand ce n'est pas nécessaire :
yield – mot-clé Python qui permet de créer des générateurs# Example of generator function
def lazy_increment(n):
for i in range(n):
yield i
f = lazy_increment(5)
next(f) # 0
next(f) # 1
next(f) # 2
Comment faire :
return par yieldyieldyield@pytest.fixture
def init_list():
return []
@pytest.fixture(autouse=True)
def add_numbers_to_list(init_list):
# Fixture Setup
init_list.extend([i for i in range(10)])
# Fixture output
yield init_list
# Teardown statement
init_list.clear()
def test_9(init_list):
assert 9 in init_list
yield au lieu de returnyieldIntroduction aux tests en Python