New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhancement: add assertDuration context manager to unittest module #85954
Comments
# Summary I propose an additional unit test type for the unittest module. # 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
# Solution I just need to add a new context manager next to this one: Line 207 in 6b34d7b
|
I think this can be very error prone because a particular operation could be interrupted by GIL switching in the middle of the context manager and be reported as taking much more than the execution of the code itself is taking. This will be very confused to users that expect it to "just work" |
I concur with Pablo. It is too unreliable. |
Thanks for the proposal Matthew, unfortunately, this addition is not reliable enough for the standard library and OTOH can be easily implemented in-situ for the users that know what the limitations are. |
Matt, you can add this to your own unit tests by just subclassing unittest.TestCase and adding a new assertDuration method. Copy the existing method's implementations (its open source and you should have the source code already, but if not you can find it here: https://github.com/python/cpython/blob/3.8/Lib/unittest/__init__.py If you are looking for some timing code to use (in case you don't already have your own) you can try this: https://github.com/ActiveState/code/tree/master/recipes/Python/577896_Benchmark_code |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: