我正在使用py.test來測試一些封裝在python類MyTester中的DLL代碼。 爲了驗證目的,我需要在測試過程中記錄一些測試數據,然後再做更多處理。由於我有很多測試_...文件,我想在我的大多數測試中重新使用測試器對象創建(MyTester的實例)。py.test:將參數傳遞給fixture函數
由於測試者對象是獲得對DLL的變量和函數的引用的對象,所以我需要將每個測試文件的DLL變量列表傳遞給測試者對象(要記錄的變量是相同的一個測試_...文件)。 列表的內容應被用來記錄指定的數據。
我的想法是做它在某種程度上是這樣的:
import pytest
class MyTester():
def __init__(self, arg = ["var0", "var1"]):
self.arg = arg
# self.use_arg_to_init_logging_part()
def dothis(self):
print "this"
def dothat(self):
print "that"
# located in conftest.py (because other test will reuse it)
@pytest.fixture()
def tester(request):
""" create tester object """
# how to use the list below for arg?
_tester = MyTester()
return _tester
# located in test_...py
# @pytest.mark.usefixtures("tester")
class TestIt():
# def __init__(self):
# self.args_for_tester = ["var1", "var2"]
# # how to pass this list to the tester fixture?
def test_tc1(self, tester):
tester.dothis()
assert 0 # for demo purpose
def test_tc2(self, tester):
tester.dothat()
assert 0 # for demo purpose
是否有可能實現它像這樣還是說沒有一個更優雅的方式?
通常我可以用某種設置函數(xUnit-style)爲每個測試方法做到這一點。但我想獲得某種重用。有沒有人知道這是可能的與固定裝置?
我知道我可以做這樣的事情:(從文檔)
@pytest.fixture(scope="module", params=["merlinux.eu", "mail.python.org"])
但我需要直接的參數化測試模塊中。 是否可以從測試模塊訪問燈具的參數屬性?
感謝提示夾具內的功能。花了一些時間,直到我可以再次工作,但這是非常有用的! – maggie