And it works. I tried to substitute unittest.mock with mocker fixture of pytest-mock and it does not work. More specifically, mock_session_class is None.
Why?
At this point, what’s the purpose of pytest-mock?
This sounds like something you should specifically ask the pytest-mock team - from the general python community on this forum you are probably not going to get that helpful responses just because it’s unlikely that there are that many people here with enough subject matter knowledge on this topic.
It seems from the documentation that the point of using pytest-mock is that it undoes the patch automatically at the end of the test, so you don’t need the with statement.
…unlike unittest.mock.patch which returns the context manager your code expects:
>>> from unittest.mock import patch
>>> listdir = patch("os.listdir", return_value="foo")
>>> listdir
<unittest.mock._patch object at 0x7ff1283d6c50>
>>> listdir("/tmp")
<function _patch.decorate_callable.<locals>.patched at 0x7ff127b85300>
>>> with listdir as mock:
... mock("/tmp")
...
'foo'
Try removing the with statement (which shouldn’t be needed if pytest-mock undoes the patch automatically) and using the the return value from mocker.patch directly: