You may have heard about unittests in the past, and most likely, like myself, didn’t really know what they were or why you would want to use them. Even if you talked with someone who did use them and loved ’em, they still had a difficult time explaining what they really were.

“In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use.”

Basically these are short little bits of code you write to test the code you are writing. Seems a little redundant, right? But it comes in real handy when your code starts getting huge, especially when you start modifying functions later on down the road. Let’s say you change an old function that has been around forever to return more data. Problem is, you don’t know exactly what code is calling the function and how this will affect that code. That is where unittests come in. If you had a unittest written for this old function you’re modifying, you could run your unittest and it would ‘test’ this function in many different ways to see if it still performed correctly. If the unittest fails, it will tell you how and why.

If you take this theory and expand upon it, and you have a decent suite of unittests accumulating, you can run these tests all the time, as your building your complex code base. Then whenever you make a small change that breaks a test, you’ll know it immediately because your unittest caught it right away and you don’t have to wait until production hits and your code breaks at a critical moment.

Luckily Python supports unittesting with a module called unittest:

“unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.”

Here’s a super-simple sample rigging function I wish I had a unittest for:

You’ll notice the “asserts” being used. Asserts are a simple way to claim that something is equal (first and second arg being equal in this case) or returns True and erroring otherwise. They seem to be the primary way to test things in unittests. In this case, I’m testing the name of the joint on line 08 and the type of the object as type ‘Joint’ on line 09.

The trick to getting unittests for all your code is to code them while you are coding your actual code. In fact, you are supposed to actually write the unittest before your actual code! Huh! That doesn’t make sense. But it does if you think about it. Your unittest will force you to consider exactly what you want your code to do and what it will return. In a way, it makes you pre-plan your code a bit. That’s the theory anyway. Long story short, try to write your unittest as you write your regular code. This takes longer at the beginning but will save you tons of time in the long run. It is truly one of the last steps towards making you a real top-notch, pro-level coder. 🙂

More for you to explore:

1. The coolest way to run these unittests is to setup a commandLine mayapy.exe session to run them for you really fast in the background. Check-out WingIDE for awesome unittesting features

NGUI for Unity by Charles Bernardoff via PACKT Publishing available here: http://bit.ly/1lp1Do7 I had the opportunity to review the first book on the mega-popular NGUI user interface plug-in for Unity. NGUI is a great UI system for < $100 that you can add into your Unity projects. It is quite in-depth and very powerful so it[…]

I was fortunate enough to stumble into a free trial of PyCharm right at the glorious 3.0 release. There was a flurry of rants and raves running through the Tech Art community upon this release and for good reason. With 3.0, JetBrains released an Open Source “Community” version which is fully featured. After using for[…]

If you’re like me, you like to always be using the latest version of an Autodesk product. The 2014 series has just been released and it is always quite a bit of work to convert you’re existing toolset up to the latest build. In the case of Maya 2014, there are more than just a[…]

This is how you subclass PyMEL’s Transform Node to create custom shapes and node types that inherit all the methods of Maya’s Transformational Objects. Subclassing PyNodes is one of the (so-called ‘experimental’) hidden gems of PyMEL. This feature allows you to take advantage of all of the factory generated methods on each Maya node type.[…]

Done Before There are dozens of useful sites and blog posts for how to incorporate PyQT into Maya (Tech-Artists.org alone has over 130 threads!) Nathan has some of the best overviews, here: nathanhorne.com I’ve found it necessary to add my own to the mix because there are a couple of tricks I’ve found to make[…]

So I’ve finally updated, modernized, and re-released the ancient, seminal introduction to python scripting for MotionBuilder. I spent quite a bit of time re-formatting it in iBooks Author and released it on the iBooks store here: and UK store: and Canada store: New features include: Member function updates for MotionBuilder 2012 ‘Interactive Images’ of scripts[…]

It occurs to me that many of you don’t yet have what is probably the best reference to date on everything python in Maya. So the blatant, condoned shameless plug comes right now: Go to http://amzn.to/mayaPythonBook and pick up a copy. A Kindle version might be interesting: http://amzn.to/mayaPythonKindle Read the quote on the back. 😉

I suspect the time has come for us to finally recognize Minecraft as a digital creation platform and not just a game. Case in point, virtual computers. Kids around the world are just now making: 16-bit computer (Arithmetic Logic Unit) 256-bit computer Dual Core CPU 16 bytes RAM Display monitor and 3D printer These technologies[…]

It seems like there are more and more posts, queries, and threads about Maya’s different scripting languages. I figure it’ll be nice to have a nice listing of the languages, their benefits and uses. Here is a quick breakdown: MEL: Entire application is basically written in MEL Super simple, easy to learn Very limited in[…]

Jason Orendorff wrote an awesome module some years ago to make it quick and easy to manipulate file paths and file names. I was fortunate enough to be exposed to this module and class via PyMEL’s package which included it. Here is our test path and we’ll slice it and dice it in all kinds[…]