Python เบื้องต้นสำหรับการทดสอบโค้ด
Alexander Levin
Data Scientist
ลองนึกถึงการเตรียมปิกนิก:
Fixture ช่วย:
สมมติว่ามี:
list ของ Python ชื่อ datadata = [0, 1, 1, 2, 3, 5, 8, 13, 21]และต้องการเทสต์ว่า:
5 และ 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
ผลลัพธ์ของตัวอย่าง:

ขั้นตอนการใช้ fixture มีดังนี้:
@pytest.fixtureสิ่งที่เรียนรู้เกี่ยวกับ testing fixture:
list ของ Pythonpytest โดยประกาศ @pytest.fixturePython เบื้องต้นสำหรับการทดสอบโค้ด