Reputation: 2183
At the top of the code I want to test I have an import like:
from resources import RESOURCES
where RESOURCES
is a dictionary of values.
How can I mock it in the test?
What I would like to is, no matter what is in the real module, return a well known dictionary.
For example in one test I want RESOURCES
to be:
{
'foo': 'bar'
}
while in another test I want it to be:
{
'something': 'else'
}
Upvotes: 8
Views: 9532
Reputation: 2183
The way I made it to patch
the RESOURCE
object is using:
from default import RESOURCES
from mock import patch
with patch.dict(RESOURCES, {'foo': 'bar'}, clear=True):
assert(RESOUCES['foo'], 'bar')
Note that you'll need to import the dictionary you want to patch in the test suite
It's also possible to use the decorator syntax:
from default import RESOURCES
from mock import patch
@patch.dict(RESOURCES, {'foo': 'bar'}, clear=True)
def test(self):
self.assert(RESOUCES['foo'], 'bar')
Upvotes: 14
Reputation: 59563
Take a look at unittest.mock.patch.object. I think that it will meet your needs.
Upvotes: 2