-
-
Notifications
You must be signed in to change notification settings - Fork 150
Description
The addition of this line (6587f79) that checks if one is inside a context manager caused our test suite to run over 3x slower (5 minutes to 17 minutes).
I understand the desire to report a more useful error message when a user tries to use it as a context manager, but the stack inspection on every patch causes a slowdown that makes pytest-mock unusable for us, especially since our test suite has hundreds of test files. Our observations indicate that the performance degradation is even more pronounced when using pytest-xdist.
One can observe the performance impacts in a simple test example:
In a test.py file:
def test_foo(mocker):
mocker.patch('random.randint')
Install pytest-repeat
and pytest-mock==1.12.0
pytest test.py --count 1000
total runtime = 4.45s
Install pytest-mock==1.11.2
pytest test.py --count 1000
total runtime = 2.28s
Now do the same experiment with pytest-xdist
with 1.12:
pytest test.py --count 1000 -n 4
total runtime = 6.67s
with 1.11.2:
pytest test.py --count 1000 -n 4
total runtime = 3.18s
Although this test case doesn't have performance impacts as pronounced as our test suite, which has hundreds of files and tests thousands of python files, it is still upwards of a 2x slowdown.
Can this check be removed altogether? IMO it is not worth it to slow down a test suite this much for a more useful error message in a few rare circumstances. Would it be possible to instead try to catch the unhelpful error message and then re-raise it as something helpful instead if this check is really aiding in the UX of the library? Thanks for your consideration