Introduktion till testning i Python
Alexander Levin
Data Scientist
Tänk dig förberedelserna inför en picknick:
Fixtures hjälper till att:
Anta att vi har:
list med namnet datadata = [0, 1, 1, 2, 3, 5, 8, 13, 21]Och vi vill testa att:
5 och 21import 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
Utdata från exemplet:

Så här använder du en fixture:
@pytest.fixtureVi har lärt oss om test-fixtures:
listpytest-fixture med dekoratorn @pytest.fixtureIntroduktion till testning i Python