I have what should've been a simple task, and it has stumped me for a while. I am trying to patch
an object imported into the current module.
Per the answers to Mock patching from/import statement in Python
I should just be able to patch("__main__.imported_obj")
. However, this isn't working for me. Please see my below minimal repro (I am running the tests via pytest
):
Minimal Repro
This is run using Python 3.8.6.
from random import random
from unittest.mock import patch
import pytest
@pytest.fixture
def foo():
with patch("__main__.random"):
return
def test(foo) -> None:
pass
When I run this code using PyCharm, I get an AttributeError
:
AttributeError: <module '__main__' from '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/_jb_pytest_runner.py'> does not have the attribute 'random'
Furthermore, when I enter debugger mode in the line before the with patch
, I see the attribute __main__
is not defined. I am not sure if it needs to be defined for patch
to work its magic.
NOTE: I know I can use patch.object
and it becomes much easier. However, I am trying to figure out how to use patch
in this question.
Research
Unable to mock open, even when using the example from the documentation
This question is related because it's both a similar error message and use case. Their solution was to use builtins
instead of __main__
, but that's because they were trying to patch
a built-in function (open
).