# test.py
from freezegun import freeze_time
from actual import DummyClass
class DummyClassMock(DummyClass):
def some_function(self):
# does something
class TestClass():
def setUp(self):
self.dummy = DummyClassMock()
@freeze_time('2021-01-01')
def test_dummy_function(self):
self.assertTrue((self.dummy.dummy_function - datetime.utcnow()).total_seconds() >= 1)
# actual.py
from datetime import datetime, timedelta
class DummyClass():
def dummy_function(self):
return datetime.utcnow() + timedelta(5)
我的代码遵循上述结构。有了这个,如果我 test_dummy_function
单独执行,dummy_function 将返回 2021-01-01 并且测试用例通过。但是,当我将它与项目中的所有其他测试用例一起运行时,它失败了。内容也不依赖。