class MyTestCase(unittest.Testcase):
def setUp(self):
self.something = True
@pytest.fixture(autouse=True)
def MyTestMethod(self, frozentime):
fn(self.something) # self.something is NOT defined
如果我使用@pytest.fixture(autouse=True)
,那么PyTest会产生一些奇怪的行为。PyTest没有在test方法之前调用我的setUp
方法,而是跳过setUp
并调用MyTestMethod
,就好像它是一个PyTest MyTestFunction
,这当然不是很好用。在
如何让MyTestMethod
使用frozentime
夹具,而不忽略应该首先调用的setUp
方法。在
这是因为autouse fixture是在
setUp
/tearDown
方法之前执行的:Source
对于这种行为你无能为力。您可以将fixture相关的代码移出
setUp
/tearDown
方法,例如:如果在类范围fixture中使用self.flag
,则可以替换与
^{pr2}$或者您可以从fixture中移动所有
setUp
相关代码:变成
相关问题 更多 >
编程相关推荐