Nhập môn Kiểm thử trong Python
Alexander Levin
Data Scientist
Hãy hình dung chuẩn bị dã ngoại:
Fixtures giúp:
Giả sử có:
list Python tên datadata = [0, 1, 1, 2, 3, 5, 8, 13, 21]Và ta muốn kiểm thử rằng:
5 và 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
Kết quả ví dụ:

Để dùng fixture, hãy làm như sau:
@pytest.fixtureChúng ta đã học về fixtures:
list Pythonpytest bằng cách khai báo @pytest.fixtureNhập môn Kiểm thử trong Python