我正在使用py.test来测试包含在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-style)为每个测试方法执行此操作 . 但我希望获得某种重用 . 有没有人知道这是否可以使用固定装置?
我知道我可以这样做:(来自文档)
@pytest.fixture(scope="module", params=["merlinux.eu", "mail.python.org"])
但我需要直接在测试模块中进行参数化 . Is it possible to access the params attribute of the fixture from the test module?
3 回答
您可以从fixture函数(以及Tester类)访问请求模块/类/函数,请参阅interacting with requesting test context from a fixture function . 因此,您可以在类或模块上声明一些参数,并且测试仪夹具可以拾取它 .
我有一个类似的问题 - 我有一个名为
test_package
的夹具,后来我想在特定测试中运行它时能够将一个可选参数传递给该夹具 . 例如:(对于这些目的而言,夹具的作用或返回的
package
的对象类型无关紧要) .然后,需要以某种方式在测试函数中使用此fixture,以便我还可以指定该fixture的
version
参数以用于该测试 . 目前这是不可能的,但可能会成为一个很好的功能 .与此同时,让我的夹具简单地返回一个完成夹具先前完成的所有工作的函数是很容易的,但允许我指定
version
参数:现在我可以在我的测试函数中使用它,如:
等等 .
OP 's attempted solution was headed in the right direction, and as @hpk42' s answer建议,
MyTester.__init__
可以存储对请求的引用,如:然后用它来实现如下夹具:
如果需要,可以对
MyTester
类进行一些重构,以便在创建后可以更新其.args
属性,以调整各个测试的行为 .这实际上是通过indirect parametrization在py.test中本地支持的 .
在你的情况下,你会有: