Introduction to Testing in Python
Alexander Levin
Data Scientist
Recall the "picnic" analogy:
It is important to clean the environment at the end of a test. If one does not use teardown, it can lead to significant issues:
When to use:
autouse
When it is not necessary to use:
yield
- is a Python keyword, which allows to create generators# 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
How to use:
return
by yield
yield
yield
@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
keyword instead of return
yield
Introduction to Testing in Python