[英]How to run setup and tear down function for all the tests in sub directories in py.test?
我有使用py.test for python 2.7和py.test 3.0運行的單元測試。 我的測試目錄是這樣的:
tests
---dir1
test1.py
-------sub-dir1-1
test-1-1.py
-------sub-dir1-2
test-1-2.py
---dir2
test2.py
-------sub-dir2-1
test-2-1.py
-------sub-dir2-2
test-2-2.py
我希望我的所有測試在測試之前和之后都能運行通用的設置和拆卸功能。 我想在所有測試代碼中進行最少的修改。
謝謝
如果我理解正確,您可以編寫一個受會話作用域的夾具( scope
參數),並使其自動使用( autouse
參數)。 我以“產量固定裝置”為例。 請注意,由於pytest 3.0和pytest.fixture
允許使用yield
, pytest.yield_fixture
不建議使用pytest.yield_fixture。
import pytest
@pytest.fixture(scope="session", autouse=True)
def callattr_ahead_of_alltests(request):
print 'run_pre_start'
yield
print 'run_after_finish'
它會在第一個測試之前運行(打印“ run_pre_start”),而在yield
之后的一部分將在所有測試之后運行(打印“ run_after_finish”)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.