我已经开始使用基于pytest的最基本的基础架构来添加其他测试类(请参见以下TestMyStuff)看来,如果我将固定装置xi和yi添加为方法setup_method的输入,它将它们称为fixtures.py中的函数,但是将它们添加到每个测试函数中,它们的返回值为和classX分别而不是函数值。两种测试方法共享需要classY和xi的相同代码块,以及每个测试额外的唯一代码。我希望采用通用代码并将其添加到yi,但是为此,我需要test_method和xi作为Fixtures方法返回的yi和classX的实例,而不是简而言之,是否有任何方法可以在所有类函数之间共享某些预先构建的装置(测试方法和prolog方法)?test_xy.pyfrom 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 yifixtures.pyfrom classY import classYfrom classX import [email protected]()def xi(request): ... return classX(request.var1, request.var2, request.var3) #classX is implemented on different file called [email protected]()def yi(request, xi) ... return classY(request.var1, xi) 最佳答案 从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我认为您无法像您期望的那样优雅地构建事物。关于python - pytest,使用fixture作为prolog方法setup_method中的类实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56847491/
10-12 14:20