Introduction to Testing in Python
Alexander Levin
Data Scientist
Imagine preparation for a picnic:
Fixtures help:
Assume we have:
list
variable named data
data = [0, 1, 1, 2, 3, 5, 8, 13, 21]
And we want to test, that:
5
and 21
import pytest
# Fixture decorator
@pytest.fixture
# Fixture for data initialization
def data():
return [0, 1, 1, 2, 3, 5, 8, 13, 21]
def test_list(data):
assert len(data) == 9
assert 5 in data
assert 21 in data
Output of the example:
To use the fixture we have to do the following:
@pytest.fixture
decoratorWe learned about testing fixtures:
list
pytest
fixture by declaring @pytest.fixture
Introduction to Testing in Python