[New-bugs-announce] [issue41788] enhancement: add assertDuration context manager to unittest module
Matthew Davis
report at bugs.python.org
Mon Sep 14 22:09:28 EDT 2020
New submission from Matthew Davis <ubuntu.one at mdavis.xyz>:
# 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
----------
components: Library (Lib)
messages: 376923
nosy: matt-davis
priority: normal
severity: normal
status: open
title: enhancement: add assertDuration context manager to unittest module
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41788>
_______________________________________
More information about the New-bugs-announce
mailing list