Question

I have found that the python internal deals with dictionary object different as the other object like function and list.

Does anyone have idea why python mock library (1.0.1) has a patch.dict specifically besides the existing patch and patch.object?

Was it helpful?

Solution

 patch.dict() for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends:

foo = {'key': 'value'}

original = foo.copy()

with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):

assert foo == {'newkey': 'newvalue'}

assert foo == original

See the reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top