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?
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.
The three most popular test runners are: unittest. nose or nose2. pytest.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With