How to setup Unity Test Tools 1.5

Creating unit tests for complicated state machines and classes is common sense. Unity Test Tools are created to help with this process. However when trying to use them in a project it turned out things are not as straightforward as I expected. In this post I will explain step-by-step how to set it up for Unity 5 and OS X, hopefully saving you hours of trial and error.

We will start from an empty project:

Screen Shot 2015-04-07 at 12.40.48

Import Unity Test Tools from the Asset Store:

Screen Shot 2015-04-07 at 12.41.11

Your project folder should now contain UnityTestTools folder and look something like this:

Screen Shot 2015-04-07 at 12.41.42

Create 2 folders in Assets. The first one must be named Editor. The second will contain dll’s and the name doesn’t matter, I name it Libs. Here’s how it looks in the project explorer:

Screen Shot 2015-04-07 at 12.42.45

Now open the UnityTestTools folder in Finder and navigate to UnitTesting->Editor->NUnit->Libs. Move the dll’s from inside and place them in the Libs folder we created in the previous step:

Screen Shot 2015-04-07 at 12.51.50

Open the Editor folder and create a new C# script. It will be our first unit test:

Screen Shot 2015-04-07 at 12.44.15Screen Shot 2015-04-07 at 12.48.25

Mono should automatically update to reference to the dll’s in the Libs folder and be able to compile the new unit test without any errors.Notice the C# script we created is not in the project that usually contains our game logic. The solution explorer should look something like this:

Screen Shot 2015-04-07 at 13.22.00

Open the Unit Test Runner (Shift + Alt + Cmd + U) and press Run All (if the new C# scrip doesn’t appear the first time try again, sometimes it doesn’t refresh right away). We now have a running unit test:

Screen Shot 2015-04-07 at 13.30.55

Why does this work

The Unity Editor automatically rewrites the references in Mono Develop and results in errors about missing assemblies. This is why we need to move the dll’s in the Assets folder, then they will be visible to all projects in our solution. If this didn’t work for you make sure the option in Preferences->Auto Refresh is enabled. If you need to use NSubstitute do the same and copy its dll to our Assets->Libs folder. If you have any suggestions, questions or comments I’d gladly discuss them in the comment section, happy unit testing!

Posted in Programming

Leave a Reply

Your email address will not be published.