Search
  • Jamie Guillemette

Unit Testing In Unity - Part 1


I've always believed the best way to explain anything is to do it and show it. So lets get started.


Setup


1. Using the top menu in Unity open the Test Runner window


The window that opens up has two mode buttons at the top: Play Mode and Edit Mode. (We will get into the difference between these shortly.




2. Click the "Create PlayMode Test Assembly Folder" button.


This will create a new folder in your Assets.

Rename this folder to PlayMode.


3. Now click on the Edit Mode button and repeat these same steps.

-Click the "Create Edit Mode Test Assembly Folder" button

-Rename the new folder to "EditMode"


4. Now let us organize this a bit. Create one more folder called "Tests" and drag the two newly created PlayMode and EditMode folders into it. The result should look something like this.




The EditMode and PlayMode files in their respective directories are assembly files. These files tell the compiler to package everything in them into a separate .dll file. This is good cause we do not want our tests being included with the production build of our games.


However, this does at gotcha for a new developer using Unity.

The first time we go to write a unit test in we will quickly discover that the scripts in our EditMode and PlayMode folders can not access the scripts outside these folders (ie the scripts we want to test in our game!)


To fix this we need to first declare an additional assembly, this time for our game and then add our game assembly as a dependency for each of the test assemblies we have made already.



Let us do a bit more clean up first in preparation. You should never build your game at the root of the Asset folder. If you do it will quickly become a mess with any other 3rd party tools or projects you may import. So let's create a new Parent Folder for our company and then our game. Move the Test folder into this new folder structure.



In this example I've also added a scripts and scenes folder and even a quick little monobehaviour script



5. Click on the Game folder and then go Create > Assembly Definition


Name this assembly the name of your game.


Great! so at this point your game will code will be bundled into your games .dll and your tests will be loaded into their own respective .dll files. Next, let's tell them about each other!


6. Click on the EditMode assembly file (this should be the only file in the EditMode folder currently). The Inspector will show a nice editor window for all the settings this file contains. File the section called "Assembly Definition References". Click the + button and drag the Game Assembly file you made in step 5 onto the new reference. click apply. Now repeat this exact step again for the PlayMode assembly file.


7. With both test assembly files updated we are now ready to create Unit tests!


(To be continued in my next blog post)

18 views

Recent Posts

See All

Unit Testing In Games - Intro

In my day job, I'm an advocate for Test-Driven Development. If you are not familiar with TDD development it is a process where developers write code that tests the business rules and functionality of

©2019 by Wind Powered Games.