我用的是py。测试一些封装在python类MyTester中的DLL代码。 为了验证目的,我需要在测试期间记录一些测试数据,然后进行更多的处理。因为我有很多考试…我想为我的大多数测试重用测试器对象创建(MyTester的实例)。
由于测试对象是一个得到DLL的变量和函数的引用,我需要为每个测试文件传递一个DLL的变量列表到测试对象(要记录的变量对于test_…文件)。 列表的内容用于记录指定的数据。
我的想法是这样的:
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风格)为每个测试方法执行此操作。但我想获得某种重用。有人知道这在固定装置上是否可行吗?
我知道我可以这样做:(来自文档)
@pytest.fixture(scope="module", params=["merlinux.eu", "mail.python.org"])
但是我需要在测试模块中直接进行参数化。 是否可以从测试模块访问夹具的params属性?