guettli
guettli

Reputation: 27879

mock dictionary at module level

I try to mock a dictionary at module level:

with mock.patch('mymodule.mydict', new_callable=mock.PropertyMock) as mock_dict:
    mock_dict.return_value={'foo': 'bar'}
    ...

But it does not work. Inside the with-statement the dictionary is empty.

I read the docs, but could not find a solution.

Any hints?

Upvotes: 4

Views: 1308

Answers (1)

Simeon Visser
Simeon Visser

Reputation: 122376

Use patch.dict to mock a dictionary:

with patch.dict('mymodule.mydict', {'newkey': 'newvalue'}):
    assert mymodule.mydict == {'newkey': 'newvalue'}

Upvotes: 4

Related Questions