What’s New in Automated Testing with Unity

Automated testing is one Automated Testing in Unitysubject I’m deeply interested in (although probably not doing enough of). I’ve already blogged about Unity Test Tools in the past, right around the time the first version was released. Since some time has passed, I thought I’d revisit the subject of automated testing with Unity to see what’s new and improved.

It’s survey time !

Before taking a look at what’s new, I thought it’d be nice to post a short survey to see what your automated testing habits look like, and how many of you are actually using automated testing (especially when working with Unity).

Click here to take the survey and view the results.

Talking About Testing

The topic of automated testing (even internally at Unity) seems to be brought up every once in a while, judging by the number of blog posts & Unite talks devoted to this matter in recent years. Check out the Resources section below for links to specific talks / blog posts.

Unity Test Tools

From what I can tell, there aren’t any “ground breaking” new features or updates to this library since I’ve last blogged about it. There has been a few releases for Unity Test Tools including various stability fixes and minor API updates. Apart from that, there hasn’t been any major news for this. Starting with Unity 5.3, the Unit test runner part is no longer a part of this package, since it comes with the engine. For more news about Unit Test Tools check out the repository or the forum thread.

Editor Test Runner

Starting with Unity 5.3 there’s a new “Editor Test Runner” window available. This is actually very similar to the “Unit Test Runner” that was before only available with Unity Test Tools. In 5.3 (or higher), this comes built-in with the Unity editor. Apart from the runner, NUnit is also bundled with the editor, so you can start writing unit tests (or any other kind of code-based tests) right away, without installing anything.

The new Editor Tests Runner window

New test scripts can be easily added by right-clicking anywhere in the Project window and selecting Create -> Editor Test C# Script:Template

 

Assertion Library

While not strictly “automated testing” related, the assertion library (added back in Unity 5.1) enables writing better, more robust code, while explicitly stating “expected” conditions throughout the code. The code for the assertion library lives under the UnityEngine.Assertions namespace (check out the documentation: Assertion Library Documentation). Assertions are nice since they are completely compiled out of the resulting final release builds, so they incur literally 0 overhead to your game.

For example, this sample code uses an assertion to make sure the velocity field is never set to 0 when the script is started:

using UnityEngine;
using UnityEngine.Assertions;

public class PlayerScript : MonoBehaviour
{
    public int velocity;

	// Use this for initialization
	void Start ()
    {
	    Assert.IsTrue(velocity != 0);
	}
}

Resources

Videos (From Unite Conferences):

 

This entry was posted in Unity and tagged , , , , . Bookmark the permalink.