Python 有没有办法在pytest函数中多次使用同一个fixture?

Python 有没有办法在pytest函数中多次使用同一个fixture?,python,python-2.7,pytest,fixtures,fixture,Python,Python 2.7,Pytest,Fixtures,Fixture,像这样 @pytest.fixture def myfixture(): # creates a complex object def test_compare(myfixture,myfixture): #compares 有没有办法知道我在做哪个固定装置? 生成的对象不同 谢谢为什么要比较fixture返回的对象?固定装置并非如此 根据文件 测试夹具的目的是提供一个固定的基线,在此基线上测试可以可靠地重复执行 如果要比较返回的对象,只需将其作为函数,而不是fixture.Li

像这样

@pytest.fixture
def myfixture():
   # creates a complex object

def test_compare(myfixture,myfixture):
   #compares
有没有办法知道我在做哪个固定装置? 生成的对象不同


谢谢

为什么要比较fixture返回的对象?固定装置并非如此

根据文件

测试夹具的目的是提供一个固定的基线,在此基线上测试可以可靠地重复执行

如果要比较返回的对象,只需将其作为函数,而不是fixture.Like

def myfixture():
   # creates a complex object

def test_compare():
   a=myfixture()
   b=myfixture()
   #compare a and b

您正在寻找工厂模式

这里有一个关于夹具工厂优势的问题

回答你的问题

@pytest.fixture
def myfixture():
   def _make_fixture():
       # creates a complex object
   return _make_fixture

def test_compare(myfixture):
    data1 = myfixture()
    data2 = myfixture()
    #compares