[TIP] Changes to mock in subversion: please try

Michael Foord michael at voidspace.org.uk
Fri Oct 22 09:44:15 PDT 2010

  Hello all,

I'll shortly be releasing *another* beta of mock 0.7.0. There are a 
handful of changes since 0.7.0 beta 3, most of which are 
straightforward. One of the changes I would like some feedback on. It is 
a *good* change, but as it is potentially backwards incompatible I would 
appreciate people trying it out and checking that it doesn't break their 

The changes are all in svn now (which will soon be a mercurial 
repository by the way):


The full list of changes from beta 3 is:

* Addition of `assert_called_once_with` method
* repr of a mock with a spec includes the class name of the spec
* `assert_called_with` works with `python -OO`
* Attempting to *set* an attribute on a class with a spec will raise an
`AttributeError` if the spec doesn't have that attribute
* Attempting to set an unsupported magic method now raises an 
* BUGFIX: mocks are now copyable (thanks to Ned Batchelder for reporting and
diagnosing this)

The one that could cause an issue is that now, if you provide a spec 
when you create your mock (either directly or through patch), not only 
will fetching arbitrary attributes not in the spec now fail but so will 
attempting to *set* an attribute not in the spec.

 >>> from mock import Mock
 >>> class X(object): pass
 >>> mock = Mock(spec=X)
 >>> mock.something = 'foo'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/compile/mock/mock.py", line 380, in __setattr__
raise AttributeError("Mock object has no attribute '%s'" % name)
AttributeError: Mock object has no attribute 'something'

This seems to be a good change as it is another way of enforcing that 
you mock objects are only used in the same way as the real objects they 
are replacing. However, if you are depending on being able to set 
attributes that don't exist in the spec then your code will fail with 
the new version of mock.

Please try out the latest version and report any problems. If there are 
no (or few) problems reported then I will release the new beta (but no 
guarantees on when as I am away in the states all next week).

All the best,

Michael Foord



READ CAREFULLY. By accepting and reading this email you agree,
on behalf of your employer, to release me from all obligations
and waivers arising from any and all NON-NEGOTIATED agreements,
licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap,
confidentiality, non-disclosure, non-compete and acceptable use
policies (”BOGUS AGREEMENTS”) that I have entered into with your
employer, its partners, licensors, agents and assigns, in
perpetuity, without prejudice to my ongoing rights and privileges.
You further represent that you have the authority to release me
from any BOGUS AGREEMENTS on behalf of your employer.

More information about the testing-in-python mailing list