Testing Your Unity C# Code


In a previous role, I was a Java developer. My team put a lot of effort into code quality. When I started learning Unity, it seemed odd not to be writing tests and running them to be sure that they passed each time. The only way to know if my code broke more than it fixed was to manually test every possible scenario.

Who has time for that? There had to be a better way.

That’s why, to meet a long-term goal of setting up a continuous delivery pipeline, I started looking into how to perform automated tests in Unity.

Code quality matters.

To get started, you’ll first want to search and import Unity’s Asset Store for “Unity Test Tools.” Then, you’ll want to create a folder somewhere in your project called “Editor.” This folder is where all your tests will go.

If the asset is installed correctly, you’ll see “Unity Test Tools” in the menu. If you have compilation errors in your project, you may not see it right away.

Next, you’ll want to open the Editor Tests Runner. This is where you run your tests from. This window may show up as an option under Unity Test Tools, but for me it was found under the Window tab.

After opening up the window, you’ll see all of the tests you have in the project. When you import the package, you’ll see a lot of sample test code to use as a guide for developing your own tests.

One option that will prove to be handy is checking “Run on recompilation.” Checking this option will run your test every time you save the project and try to play it in the editor. You can choose to leave this option unchecked if you’d rather click the “Run All,” “Run Selected,” or “Rerun Failed” options.

Once you have your Editor Tests window ready, it’s time to write some tests. Create a script in your “Editor” folder for your test class. (Note: It must be in an “Editor” folder somewhere in your project for the test tools to find it.)

Some things to note about the test script:

  1. You’ll need to add an import at the top. “using NUnit.Framework;”
  2. Use the annotation [TestFixture] before declaring the test class.
  3. Each test method you write will need the [Test] annotation.
  4. Write your test methods and use “Assert…” statements to test your code. If you’re new to unit testing and the types of assert statements, this can be a helpful link: http://www.nunit.org/index.php?p=quickStart&r=2.6.4

Once you’ve written your tests, go back to the Editor Tests and click an option to run them. The reasons for failing tests will show up on the bottom when selecting a failed test.

Optional: You can add a [SetUp] annotation on a setup method if you want to set up any variables or anything that’s common to all your tests.

Optional: If you’re writing a test that throws an assertion, you can use the same steps as above but add an additional annotation.
[ExpectedException (typeof(), ExpectedMessage=”Your message here”)]

Getting started with unit testing in Unity is pretty quick with Unity Test Tools. Import the package, create your “Editor” folder, and try it yourself.

Kristin is a freelance developer living in the San Francisco Bay Area. Her development background includes a mix of LAMP stack web development, Java and Android development, as well as Unity C# development. She is currently working on the Virtual Reality Developer Nanodegree from Udacity.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

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

Skip to toolbar