I have an object MyObject which uses a constant defined in mymodule.constants.MYCONSTANT. I can successfully patch the constant using a context manger like so:
import pytest
from unittest.mock import patch
def test_constant(self):
with patch('mymodule.constants.MYCONSTANT', 3):
MyObject()
However, I can't figure out how to use the equivalent patching using patch as a decorator:
@patch('mymodule.constants.MYCONSTANT', 3)
def test_constant(self, mock_constant):
MyObject()
The above fails with a fixture mock_constant not found error. I tried using
@patch('mymodule.constants.MYCONSTANT', return_value=3)
But MYCONSTANT doesn't get replaced with the value of 3.