Introducere în testarea în Python
Alexander Levin
Data Scientist
Reamintiți-vă analogia cu „picnicul":
Este important să curățați mediul la finalul unui test. Neutilizarea teardown-ului poate genera probleme semnificative:
Când se utilizează:
autouseCând nu este necesar:
yield - cuvânt cheie Python care permite crearea de generatoare# 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
Cum se utilizează:
return cu 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 în loc de returnyieldIntroducere în testarea în Python