We have an ancient thread about the possibility of using test mocks for maya unit tests
I’ve been looking at my test coverage and thinking that it would be good to run hard with mocks – for a number of reasons, including making it easier to manage a build server that could run without Maya. My feeling is that most unit tests really only need to verify that you’ve asked maya to do something and that you respond appropriately when Maya sends back a particular result.
Before I invest a bunch of time in this, has anybody in the four years since the last thread actually done this? Do you have any experience or insights you’d care to share?
I did a quick test and this appears to work:
import sys
import mock
from types import ModuleType
_maya = ModuleType('maya')
_cmds = ModuleType('cmds')
_maya.cmds = mock.MagicMock()
sys.modules['maya'] = _maya
sys.modules['maya.cmds'] = _cmds
# the second module exists only to enable imports,
# we actually use the mock object in the _maya module
# now you can do this:
import maya.cmds as cmds
print cmds.ls()
# <MagicMock name='mock.ls()' id='51113168'>
cmds.ls.side_effect = ['pCube1']
print cmds.ls()
# pCube1
However, I have not yet gotten around to trying to plumb it through a test suite yet. So – anything to look out for ?
In the meantime, more about mocking for the curious.