簡體   English   中英

如何在py.test子目錄中的所有測試中運行安裝和拆卸功能?

[英]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允許使用yieldpytest.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM