I have a method to calculate the number of ms between two times.
def time_diff(orig_time):
return int((time.time() - orig_time) * 1000)
It is used similar to:
orig = time.time()
# do stuff
time_duration = time_diff(orig)
I understand this is not necessarily ideal from a repeatability perspective, because time.time()
has some accuracy issues.
From a practical perspective, this function works good enough. I don't need exact precision here and know I lose precision.
However, my issue is when I go to test services relying on this functionality.
It seems I have a few options:
class ReportingTest(TestCase):
def test_time_diff_badly(self):
# this approach is bad and I should not even consider it
orig = time.time()
self.assertEqual(
time_diff(orig),
0)
@patch('time.time')
def test_time_diff_mocked(self, time_mock):
time_mock.return_value = 0
self.assertEqual(
time_diff(0.0015),
1.5)
def test_time_diff_threshold(self):
orig = time.time()
dt = time_diff(orig)
self.assertLessEqual(dt, 0.10)
self.assertGreaterEqual(dt, 0)
The first is clearly bad. There is no value I can put which will consistently return. Mocking seems workable as does the threshold based approach. I don't really like mocking though since it seems a bit pointless to even write the test if I do that.
Is there a better way to test this function?