Unit Testing in Unity

[Update: The project is now available on Github. Feel free to contribute! :) ]

For a while now, I’ve had this project sitting in an unfinished state and figured it was time I gave it a tidy up and made it available for people to use.

A quick disclaimer before I go into the details: I started the project with the hopes of putting this as a package on the Unity Asset Store. However with a lot of other projects and responsibilities, I don’t have the time to dedicate to the project to make it as good as it could be and support it after release. As such, I’m making it available here for free in a working but basic state which can be used in your projects freely. I’ve given the code a quick clean-up so it should hopefully be easy for people to work with. It’s far from finished/perfect but I’ve tested this in Unity 4.2 Pro on a Mac and it seems to work fine. While I don’t have time to actively support this project at present, if anyone does spot any major issues or adds improvements, I would love to hear about them!

Here’s what the tool currently looks like in Unity 4.2 Pro:

UnityUnitTesting-Window

So, on with the details…

 

A Bit of Background (why I made it)

As a developer, I feel much more secure when using test driven development. Having tests which automatically run and alert you to any issues as you expand/refactor your codebase is incredibly valuable and implementing them saves a lot of time in the long run. Unfortunately, for Unity projects there hasn’t been much in the way of utils/libraries which support unit testing. There are a number of unit testing frameworks available to use, but they require manual setup and provide no Unity-specific utilities to make them simple to use.

This is what my UnityUnitTesting package was intended to solve. I have taken NUnit Lite, added some simple wrapper scripts and implemented a UI to enable you to run and visualise your unit test results from within the Unity editor. The library has the ability to watch your code for changes and automatically run your unit tests whenever your scripts are recompiled (after you see the spinner in the bottom-right of the unity window). This removes the manual step and prevents you forgetting to run them. If you don’t want the editor window visible all the time, the system still logs an error or warning for any broken/ignored/skipped tests it finds.

I’ve tried with this system to have it auto-detect your tests so you shouldn’t have to do any set up. Just add the package to your project, write your tests and have them run. Again on the disclaimer front – I’ve only done limited testing of this, but the code is simple enough that you should be able to easily customise/patch it for your specific requirements. It currently works by browsing the two assemblies you’re likely to be working with (“Assembly-CSharp”, “Assembly-CSharp-Editor”) for any available test fixtures. It then runs all your tests on a per-assembly basis. If you wish to add/remove an assembly, you can do so by adding it to the _assemblyNamesToTest HashSet at the top of the UnityUnitTester class. For most of you though, you hopefully shouldn’t have to change anything.

 

Setup

You can download the package from this link.

Once you’ve added the package to your project (Assets -> Import Package -> Custom Package…), you should see the following folder/files added to your project:

UnityUnitTesting-Project

As you’ll see from the screenshot, I’ve added four TestFixture classes as examples. Here you’ll find a lot of example unit tests making use of various options in the NUnitLite API. It’s worth keeping these files initially so you can ensure the system works and can even use them as a template for your own tests. Once you’re happy with the setup, feel free to delete all of the example tests.

To view the editor window and test results display, go to Window -> UnitTesting -> ShowWindow:

UnityUnitTesting-Menu

This will display the unit testing results window which has options to manually run the tests or disable the auto-running of them. Below is what you should see if you’ve kept the example test fixtures. I’ve expanded the results in the screenshot to show the details:

UnityUnitTesting-Window

From this screenshot, you can see that any failed tests or errors are shown in red. Anything which was skipped or ignored is shown in yellow. I have removed highlighting of passed results (they used to be green) to help these other tests stand out, however that would be easy for you to re-implement should you wish to have that feature back. With the highlighting set up this way, you can quickly browse the collapsed list of fixtures and easily spot any red lines to help you quickly identify failing tests.

If you wish to disable the automatic running of tests, you simply need to check the checkbox. This setting should be saved in your preferences automatically (again, disclaimer, I’ve only given that a very quick test).

The window will no doubt get rather large as you add more tests. In case you’d rather not see it all the time, I’ve set the system to debug log a summary of the test results. The way I tend to work is to have the window hidden and only show it if I spot an error/warning in my console:

UnityUnitTesting-Console

And that’s it! I hope someone finds this project useful! :)

 

Useful References / Links

 

  1. #1 by Andrew on December 2, 2013 - 4:23 pm

    Hey, been messing around with this and I like what you did. However it’s got no licensing included in the file, so it’s hard to use this commercially.

    With your permission, I’d like to host it on Github under an MIT licence (http://choosealicense.com/licenses/mit/), so people can contribute to the project.

  2. #3 by phime studio on November 4, 2013 - 6:38 pm

    Hey Kostya – she’s a girl LOL :)

    Anyway, good job Cat on this one !

  3. #4 by Kostya on November 1, 2013 - 11:09 pm

    Thanks, man! I’ve been trying to get into TDD in Unity for sometime now, and your package seems like a great start!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>