Nhập môn Kiểm thử trong Python
Alexander Levin
Data Scientist
Nhớ ví dụ "dã ngoại":
Cần dọn môi trường sau mỗi test. Nếu không dùng teardown, có thể gây ra:
Khi dùng:
autouseKhi không cần:
yield là từ khóa Python giúp tạo generator# Ví dụ hàm generator
def lazy_increment(n):
for i in range(n):
yield i
f = lazy_increment(5)
next(f) # 0
next(f) # 1
next(f) # 2
Cách dùng:
return bằng yieldyieldyield@pytest.fixture
def init_list():
return []
@pytest.fixture(autouse=True)
def add_numbers_to_list(init_list):
# Fixture Setup
init_list.extend([i for i in range(10)])
# Fixture output
yield init_list
# Teardown statement
init_list.clear()
def test_9(init_list):
assert 9 in init_list
yield thay cho returnyieldNhập môn Kiểm thử trong Python