Pytest for Nuke tools from the commandline

Pytest is a very cool (because easy to use) framework for writing automated tests in Python. It is much less verbose than the built-in unittest module, yet has the same (if not more) functionality and is plain great when it comes to detailled error output on failing tests.

As I have just started diving into the world of unittesting and test-driven development, I however found it especially helpful to not have to write all the boilerplate code that you would need with the unittest module. Just create a file that start with test_ and has some functions in it with the same prefix, and pytest will find and execute it automatically when called on any point above. The test output on failure is also much more helpful for debugging.

When writing tests for tools working with the Nuke API however, there is a problem. Since Nuke version 8.x you can use import nuke directly in a script without having to start Nuke at all, but in prior versions you are required to do that. As my tests are for Nuke 6.x I can not start them the usual way from the commandline likepy.test tests/ , as import nuke will fail.

My workaround was to open the Nuke GUI manually and start pytest from a script editor like:

import pytest
pytest.main(['-vv', 'path/to/tests/'])

As you can imagine, that is not very convenient.Instead I copied the snipped to a python file called and created a runTests.bat that calls this script using the Nuke python interpreter like:

"C:\Program Files\Nuke 6.x\Nuke6.x.exe" -t path/to/

By calling it this way, import nuke works. Binding the runTests.bat to an alias in your shell (e.g. nuketests) makes it a oneliner to call your nuke-pytests from commandline:

Nuke is still required to launch on every testrun, but only on your commandline, which is much faster than opening the GUI and you still get the usual colored pytest output 🙂

The setup is a bit hacky, but I found it to speed up my testing workflow quite a bit, and you only have to do it once. Of course your alias could be the python call directly instead, I just found it useful to have a .bat that I could use from outside of a shell.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.