Is there an easy way to mock loosely defined dict objects in Python? For example, how can I easily express that given a dict input
, I want to check whether or not each value in it conforms to a particular meta-definition, like minimum and maximum values, lengths, and types?
Being able to do this could be handy, for example, when writing tests.
In mock (unittest.mock in Python versions 3.3+) one can specify that a value can be ANY
value, like in
>>> mock = Mock(return_value=None)
>>> mock('foo', bar=object())
>>> mock.assert_called_once_with('foo', bar=ANY)
However, what if bar
above should be a dict-like object, like
>>> {'baz': <an integer between -3 and 14>, 'qux': <'yes' or 'no'>}