[openstack-dev] [nova] Do we have some guidelines for mock, stub, mox when writing unit test?

Matt Riedemann mriedem at linux.vnet.ibm.com
Sun Nov 10 15:18:16 UTC 2013


I don't see anything explicit in the wiki and hacking guides, they 
mainly just say to have unit tests for everything and tell you how to 
run/debug them.

Generally mock is supposed to be used over mox now for python 3 support.

There is also a blueprint to remove the usage of mox in neutron:

https://blueprints.launchpad.net/neutron/+spec/remove-mox

For all new patches, we should be using mock over mox because of the 
python 3 support of mock (and lack thereof for mox).

As for when to use mock vs stubs, I think you'll get different opinions 
from different people. Stubs are quick and easy and that's what I used 
early when I started contributing to the project, but since then have 
preferred mox/mock since they validate that methods are actually called 
with specific parameters, which can get lost when simply stubbing a 
method call out. In other words, if I'm stubbing a method and doing 
assertions within it (which you'll usually see), if that method is never 
called (maybe the code changed since the test was written), the 
assertions are lost and the test is essentially broken.

So I think in general it's best to use mock now unless you have a good 
reason not to.

On 11/10/2013 7:40 AM, Jay Lau wrote:
> Hi,
>
> I noticed that we are now using mock, mox and stub for unit test, just
> curious do we have any guidelines for this, in which condition shall we
> use mock, mox or stub?
>
> Thanks,
>
> Jay
>
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>

-- 

Thanks,

Matt Riedemann




More information about the OpenStack-dev mailing list