【发布时间】:2009-06-26 14:49:52
【问题描述】:
我需要为返回字典的函数编写单元测试。这本字典中的一个值是datetime.datetime.now(),它当然会随着每次测试运行而变化。
我想在我的断言中完全忽略该键。现在我有一个字典比较功能,但我真的很想像这样使用 assertEqual:
def my_func(self):
return {'monkey_head_count': 3, 'monkey_creation': datetime.datetime.now()}
... unit tests
class MonkeyTester(unittest.TestCase):
def test_myfunc(self):
self.assertEqual(my_func(), {'monkey_head_count': 3}) # I want to ignore the timestamp!
是否有任何最佳实践或优雅的解决方案可以做到这一点?我知道assertAlmostEqual(),但这仅对浮动 iirc 有用。
【问题讨论】:
标签: python unit-testing