Python 테스트 입문
Alexander Levin
Data Scientist
"피크닉" 비유:
테스트 끝에 환경을 정리하는 것이 중요합니다. teardown을 사용하지 않으면 심각한 문제가 생길 수 있습니다:
사용 시점:
autouse 사용불필요한 경우:
yield는 제너레이터를 만드는 Python 키워드입니다# Example of generator function
def lazy_increment(n):
for i in range(n):
yield i
f = lazy_increment(5)
next(f) # 0
next(f) # 1
next(f) # 2
사용 방법:
return을 yield로 바꾼다yield 뒤에 정리 코드를 둔다yield 이전에만 둔다@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
return 대신 yield 사용yield 뒤에 정리 코드 배치Python 테스트 입문