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:
Import Unity Test Tools from the Asset Store:
Your project folder should now contain UnityTestTools folder and look something like this:
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:
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:
Open the Editor folder and create a new C# script. It will be our first unit test:
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:
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:
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!