6

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?

Soviut
  • 88,194
  • 49
  • 192
  • 260

1 Answers1

15

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')
Soviut
  • 88,194
  • 49
  • 192
  • 260
Moe
  • 28,607
  • 10
  • 51
  • 67
  • How do I tell Nose to use the mayapy.exe as its interpreter? I'm running it from the command line. – Soviut Mar 13 '09 at 06:08
  • run it as follows % mayapy nosetests either that or modify the #! line to be #! /path/to/mayapy and then just run it as: % nosetests – Moe Mar 13 '09 at 12:32
  • 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 Mar 13 '09 at 15:45
  • I took the liberty of editing your answer to additional instructions. – Soviut Mar 13 '09 at 15:52