我想在測試套件中的每個測試之前和之後運行額外的安裝和拆卸檢查。我看過燈具,但不知道他們是否是正確的方法。我需要在每次測試之前運行設置代碼,並且我需要在每次測試之後運行拆卸檢查。在py.test的每個測試之前和之後運行代碼?
我的用例是檢查沒有正確清理的代碼:它會留下臨時文件。在我的設置中,我將檢查這些文件,並在拆解過程中檢查這些文件。如果有額外的文件,我想測試失敗。
我想在測試套件中的每個測試之前和之後運行額外的安裝和拆卸檢查。我看過燈具,但不知道他們是否是正確的方法。我需要在每次測試之前運行設置代碼,並且我需要在每次測試之後運行拆卸檢查。在py.test的每個測試之前和之後運行代碼?
我的用例是檢查沒有正確清理的代碼:它會留下臨時文件。在我的設置中,我將檢查這些文件,並在拆解過程中檢查這些文件。如果有額外的文件,我想測試失敗。
您可以使用的裝飾,但編程,所以你不需要把裝飾每種方法。
我假設幾件事情在下面的代碼:
的測試方法都命名,如:「的testXXX()」 裝飾器被添加到測試方法都實現了相同的模塊。
def test1():
print ("Testing hello world")
def test2():
print ("Testing hello world 2")
#This is the decorator
class TestChecker(object):
def __init__(self, testfn, *args, **kwargs):
self.testfn = testfn
def pretest(self):
print ('precheck %s' % str(self.testfn))
def posttest(self):
print ('postcheck %s' % str(self.testfn))
def __call__(self):
self.pretest()
self.testfn()
self.posttest()
for fn in dir() :
if fn.startswith('test'):
locals()[fn] = TestChecker(locals()[fn])
現在,如果你調用的測試方法...
test1()
test2()
輸出應該是這樣的:
precheck <function test1 at 0x10078cc20>
Testing hello world
postcheck <function test1 at 0x10078cc20>
precheck <function test2 at 0x10078ccb0>
Testing hello world 2
postcheck <function test2 at 0x10078ccb0>
如果你有測試方法類的方法,該方法是也有效。例如:
class TestClass(object):
@classmethod
def my_test(cls):
print ("Testing from class method")
for fn in dir(TestClass) :
if not fn.startswith('__'):
setattr(TestClass, fn, TestChecker(getattr(TestClass, fn)))
到TestClass.my_test()
呼叫將打印:
precheck <bound method type.my_test of <class '__main__.TestClass'>>
Testing from class method
postcheck <bound method type.my_test of <class '__main__.TestClass'>>
這看起來好像可能適用於免費的功能。我也有班級fucntions(雖然我試圖擺脫所有的測試類)。 –
它也適用於類方法,我已經更新了我的答案。 – Roberto
py.test燈具要實現你的目的技術上足夠的方法。
你只需要定義一個這樣的夾具:
@pytest.yield_fixture(autouse=True)
def run_around_tests():
# Code that will run before your test, for example:
files_before = # ... do something to check the existing files
# A test function will be run at this point
yield
# Code that will run after your test, for example:
files_after = # ... do something to check the existing files
assert files_before == files_after
通過與autouse=True
聲明你的燈具,它會自動調用了同樣的模塊定義的測試功能。
這就是說,有一個警告。斷言設置/拆卸是一個有爭議的做法。我的印象是,py.test的主要作者不喜歡它(我也不喜歡它,所以這可能會使我自己的感覺變成顏色),所以當你前進時,你可能遇到一些問題或粗糙的邊緣。
注意:此語法自3.0起折舊。使用'@ pytest.fixture'。 –
這裏是與上一條評論相關的[鏈接](https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code)。 – Paolo
您可以使用Pytest的Module level setup/teardown Fixtures。
這裏的鏈接
http://pytest.org/latest/xunit_setup.html
其工作原理如下:
def setup_module(module):
""" setup any state specific to the execution of the given module."""
def teardown_module(module):
""" teardown any state that was previously setup with a setup_module
method."""
Test_Class():
def test_01():
#test 1 Code
它將調用setup_module
測試前和測試teardown_module
完成後。
您可以在每個測試腳本中包含此夾具,以便爲每個測試運行該夾具。
如果你想使用的東西是共同的所有測試目錄中您可以使用包/目錄級別賽事的鼻子框架
http://pythontesting.net/framework/nose/nose-fixture-reference/#package
在__init__.py
文件的包,你可以包括以下
def setup_package():
'''Set up your environment for test package'''
def teardown_package():
'''revert the state '''
燈具正是你想要的。 這就是他們的設計目的。
無論您使用pytest風格燈具,或設置和拆卸(模塊,類或方法級別)的xUnit風格的燈具,依賴於環境和個人喜好。
從您所描述的內容來看,您似乎可以使用pytest autouse fixtures。
或xUnit風格功能級別setup_function()/teardown_function()。
Pytest已經完全覆蓋。如此之多,以至於它可能是信息的消防軟管。
我不想自己標記每個函數。我想要一些適用於模塊內所有測試功能的東西。 –