This repository is designed to guide individuals with no or little experience in using pytest and mocks to test Maya Python plugins. The testing framework provided here leverages pytest, a powerful tool for writing and executing tests in Python. This guide assumes that you are working with Maya 2022.5 or superior, but the principles are applicable to other versions with some adjustments.
The plugin featured in this repository is a simple utility designed for Autodesk Maya. It creates a custom Maya node that calculates the center point among a list of object positions. The positions are provided using the world matrix of each object. Although there are alternative methods to achieve this in Maya, the purpose of this plugin is to offer a straightforward example to demonstrate the testing process of Maya Python plugins with pytest and mocks.
This plugin serves as an illustrative example for those learning how to test Maya Python plugins, emphasizing the simplicity and directness of the process rather than the practicality of the plugin itself.
- Maya 2022.5: The tests are created and tested on Maya 2022.5. Compatibility with older versions is not guaranteed.
- Pytest: A Python testing tool required to run the tests in this repository.
To install pytest, follow these steps:
- Open your terminal.
- Navigate to the
maya[version]/bin
folder. Replace[version]
with your actual Maya version number. - Run the command:
mayapy -m pip install pytest
- To verify the installation, run:
mayapy -m pip list
and check if pytest is listed.
To run the tests, it's advised to add the mayapy
folder to your system's environment variables. But it can be run using the maya full path as well. Here are the instructions for Windows, macOS, and Linux:
- Search for "Environment Variables" in the Start menu and select "Edit the system environment variables."
- In the System Properties window, click on "Environment Variables."
- Under System Variables, find the
Path
variable and select it. Click "Edit." - Click "New" and add the path to your
mayapy
folder. - Click "OK" on all windows to save your changes.
- Open Terminal.
- Edit your shell profile file (e.g.,
~/.bash_profile
,~/.zshrc
, etc.) using a text editor. - Add the following line:
export PATH="/path/to/mayapy:$PATH"
. Replace/path/to/mayapy
with the actual path to yourmayapy
folder. - Save the file and close the editor.
- In Terminal, run
source ~/.bash_profile
(or the equivalent file you edited) to apply the changes.
- Open Terminal.
- Edit your shell profile file (e.g.,
~/.bashrc
,~/.zshrc
, etc.) using a text editor. - Add the following line:
export PATH="/path/to/mayapy:$PATH"
. Replace/path/to/mayapy
with the actual path to yourmayapy
folder. - Save the file and close the editor.
- In Terminal, run
source ~/.bashrc
(or the equivalent file you edited) to apply the changes.
To run your tests, use the following command in the terminal:
mayapy -m pytest [name of the file]
For example, to test the test_center_point_node.py
file, you would run:
mayapy -m pytest -s tests/unit/test_center_point_node.py