I need to write a unit test for a function that returns a dictionary. One of the values in this dictionary is datetime.datetime.now()
which of course changes with every test run.
I want to ignore that key completely in my assert. Right now I have a dictionary comparison function but I really want to use assertEqual like this:
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!
Is there any best practices or elegant solutions for doing this? I am aware of assertAlmostEqual()
, but that's only useful for floats iirc.