Just wanted to share with you all an open source Unit Testing Framework I’ve been working on. It works within Unity itself by going to the “Window > Unit Testing” menu item.
NOTE 1: Make sure you wrap your tests in one big #if UNITY_EDITOR or else your game will not build
NOTE 2: Feel free to contribute
For a quick example of what a Unit Test script might look like:
#if UNITY_EDITOR
using UnitTesting;
using UnityEngine;
public class ExampleUnitTestingScript
{
GameObject dummyGameObject;
const string objectName = "Dummy";
[SetUp]
public void HandleSetup()
{
dummyGameObject = new GameObject(objectName);
}
[TearDown]
public void HandleTearDown()
{
GameObject.DestroyImmediate(dummyGameObject);
}
[Test]
public void DummyGameObjectNameShouldBeDummy()
{
Assert.AreEqual(objectName, dummyGameObject.name);
}
[Test]
public void SlightlyOverPointFiveOfZeroToOneLerpShouldBeApproxPointFive()
{
float result = Mathf.Lerp(0.0f, 1.0f, 0.505f);
// If you change the following 0.01f to 0, you're test should fail
Assert.Approx(0.5f, result, 0.01f);
}
}
#endif
That’s a really good question, the problem is that all of my real world examples are either under NDA or are products that are for sale and you’d need the product to use the tests.
Most of the run time portions of the AI Behaviors Made Easy system have Unit Tests. Unfortunately, you’d need to buy it in order to even use the tests. If you have a copy, I’d be happy to send you the tests for it
With that said, this system works with anything that Unity supports such as physics, transforms, default and custom components, etc… For instance, in the example above you can use AddComponent and store the component to a class variable in the SetUp method and then do whatever tests you want in your test methods.
Maybe when I get time, I’ll write a good real world example that I can legally provide in the main source code. For now, the current provided example should be a really good start if you’re familiar with unit testing and especially if you’re familiar with NUnit.
I second that… I was a bit skeptical at first, especially from the game development side of things, but once I got using it I can honestly say that there’s no reason to shy away from it
I’m glad you’ve found this system useful. The reason why it uses an IEnumerator is so that you can see progress, otherwise it’ll looked locked up if you have a lot of tests, especially if you need to do things like spawning GameObjects and adding components as these incur a fairly large overhead compared to just raw classes and such. The way to avoid the null reference exception is to make sure no tests are running while scripts are recompiling. I’ll try and see if there is a solution to this issue and fix it… unless you know of one and want to fix it and commit it up.
Thanks again and I’m glad you’re enjoying using it
Nathan