Pythonによるテスト入門
Alexander Levin
Data Scientist
「ピクニック」の例え:
テストの最後に環境をクリーンアップすることが重要です。ティアダウンをしないと、次の問題を招きます:
使う場面:
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によるテスト入門