New submission from Matthew Davis <ubuntu....@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue41788>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to