Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running unit tests with Nose inside a Python environment such as Autodesk Maya?

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?

like image 206
Soviut Avatar asked Mar 12 '09 17:03

Soviut


People also ask

What is Nose test in Python?

Nose is a popular test automation framework in Python that extends unittest to make testing easier. The other advantages of using the Nose framework are the enablement of auto discovery of test cases and documentation collection.

What are the test runners that we can use in Python?

The three most popular test runners are: unittest. nose or nose2. pytest.

Can you use Python in Maya?

You can also enter short Python commands into the command-line. A toggle allows you to enter either MEL or Python commands. You can -drag Python scripts to the Shelf, just like MEL scripts.

Why unit testing is important?

The prime purpose of unit testing is to separate the written code for testing and determine if it works as intended. Unit testing involves testing individual components of a software program or application. The main objective of this process is to check that all the individual units are working in an intended way.


1 Answers

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')
like image 50
Moe Avatar answered Sep 20 '22 12:09

Moe