Message376923
# Summary
I propose an additional unit test type for the unittest module.
TestCase.assertDuration(min=None, max=None), which is a context manager, similar to assertRaises. It runs the code inside it, and then fails the test if the duration of the code inside was not between min and max.
# Use case
I want to check that when I call a certain function in my application, it doesn't take far more time than I expect, or far less time.
e.g. if I'm trying to do things concurrently, I want to test that they are indeed concurrent, by measuring whether the duration equals the sum of all processes' duration, or the max.
# MWE
```
import unittest
from time import sleep, time
from multiprocessing import Pool
def slow(x):
sleep(x)
# blocking sleep for 0, 1, 2, 3 seconds, concurrently
def together():
with Pool(4) as p:
p.map(slow, range(4))
class TestConcurrent(unittest.TestCase):
# this is how you do it today
def test_today(self):
start = time()
together()
end = time()
duration = end - start
self.assertGreaterEqual(duration, 2)
# max should be 3 seconds, plus some overhead
# if together() called slow() in series,
# total duration would be 0 + 1 + 2 + 3 = 6 > 4
self.assertLessEqual(duration, 4)
# this is how I want to do it
def test_simpler(self):
with self.assertDuration(min=2, max=4):
together()
```
# Solution
I just need to add a new context manager next to this one:
https://github.com/python/cpython/blob/6b34d7b51e33fcb21b8827d927474ce9ed1f605c/Lib/unittest/case.py#L207 |
|
Date |
User |
Action |
Args |
2020-09-15 02:09:28 | matt-davis | set | recipients:
+ matt-davis |
2020-09-15 02:09:28 | matt-davis | set | messageid: <1600135768.52.0.225674115318.issue41788@roundup.psfhosted.org> |
2020-09-15 02:09:28 | matt-davis | link | issue41788 messages |
2020-09-15 02:09:27 | matt-davis | create | |
|