This post covers assumes that you are already familiar with the concepts behind test driven development and focuses on how you can use the Nose testing framework to incorporate tdd into maya.
A few assumptions:
- You have already installed nose somewhere on your system
- Your path environment variables have already been setup so that maya can find the nose module
- Your path environment variables have already been setup so that maya can find your script modules
For the purposes of this example lets say that all your scripts are stored under a directory called lot_tools. lot_tools is a standard python package with two sub modules: example_a and example_b. On disk, that would look like this:
All of the actual work of unit testing is done in the example_a_tests.py. You can have as many test files as you like because nose is very flexible in the discovery of test files. Typically, I will develop a unique test file per file in my module.
# import your module
# import nose
# create an instance of a test loader
# a test loader is responsible for discovering tests
test_loader = nose.loader.TestLoader()
# load the tests from our module
test_suite = test_loader.loadTestsFromModule(lot_tools.example_a)
# run them
If your module fails the tests and you need to make changes, simply make your changes, reload the module and re-run the tests.
If you need to change your tests you will need to restart Maya after you update the test files. This is because the tests are also python modules and I have not figured out the correct module path to reload them. This is normally fine for development purposes because you will normally write your tests first, then write the code to pass the tests.
If you want to run tests on more then one module, you can use nose to both load the modules and the tests at the same time.