Python 测试入门
Alexander Levin
Data Scientist
想象一次野餐准备:
夹具的作用:
假设我们有:
data 的 Python list 变量data = [0, 1, 1, 2, 3, 5, 8, 13, 21]我们要测试:
5 和 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
示例输出:

要使用夹具,请执行:
@pytest.fixture 装饰器我们学习了测试夹具:
list@pytest.fixture 定义 pytest 夹具Python 测试入门