我已经开始使用基于pytest的最基本的基础架构来添加其他测试类(请参见TestMyStuff
)
似乎如果我将固定装置xi
和yi
添加为方法setup_method
的输入,它将它们称为fixtures.py
中的函数,但将它们添加到每个测试函数,它们分别获得返回值classX
和classY
而不是函数值。
两种测试方法共享相同的代码块,需要xi
和yi
,以及每个测试的唯一唯一代码。
我希望采用通用代码并将其添加到test_method
中,但是为此,我需要xi
和yi
作为classX
和classY
的固定装置实例方法返回,而不是fixtures.py
简而言之,是否有任何方法可以在所有类函数(测试方法和prolog方法)之间共享一些预先构建的装置?
test_xy.py
from fixtures import *
class TestMyStuff
def setup_method(self, test_method, xi, yi):
# here xi and yi are functions :-(
self.x = classX.makeX()
self.y = classY.makeY()
def test_X(self, xi, yi):
# here xi and yi are instances of classX and classY
# do shared stuff with xi and yi
# do unique stuff with xi and yi
def test_Y(self, xi,yi):
# here xi and yi are instances of classX and classY
# do shared stuff with xi and yi
# do other stuff with xi and yi
fixtures.py
from classY import classY
from classX import classX
@pytest.fixture()
def xi(request):
...
return classX(request.var1, request.var2, request.var3)
#classX is implemented on different file called classX.py
@pytest.fixture()
def yi(request, xi)
...
return classY(request.var1, xi)
答案 0 :(得分:0)
来自pytest文档:
You can mix both fixture mechanisms in the same file but test methods of unittest.TestCase subclasses cannot receive fixture arguments.
https://docs.pytest.org/en/latest/xunit_setup.html
我认为您无法像您期望的那样优雅地构建事物。