views:

780

answers:

1

I'd like to start creating unit tests for my Maya scripts. These scripts must be run inside the Maya environment and rely on the maya.cmds module namespace.

How can I run Nose tests from inside a running environment such as Maya?

+4  A: 

Use the mayapy executable included in your maya install instead of the standard python executable.

In order for this work you'll need to run nose programmatically. Create a python file called runtests.py and put it next to your test files. In it, include the following code:

import os
os.environ['PYTHONPATH'] = '/path/to/site-packages'

import nose
nose.run()

Since mayapy loads its own pythonpath, it doesn't know about the site-packages directory where nose is. os.environ is used to set this manually inside the script. Optionally you can set this as a system environment variable as well.

From the command line use the mayapy application to run the runtests.py script:

/path/to/mayapy.exe runtests.py

You may need to import the maya.standalone depending on what your tests do.

import maya.standalone
maya.standalone.initialize(name='python')
Moe
How do I tell Nose to use the mayapy.exe as its interpreter? I'm running it from the command line.
Soviut
run it as follows% mayapy nosetestseither that or modify the #! line to be#! /path/to/mayapyand then just run it as:% nosetests
Moe
Thanks, I'm not sure why I didn't clue in to that. I guess I was in the mindset that it was some kind of nosetest flag itself.
Soviut
I took the liberty of editing your answer to additional instructions.
Soviut
cool - glad to hear that it works.
Moe
Edited the code yet again with my current working example.
Soviut