Unity unit test framework focused on run play time and actual machine
Presentation Slide - RuntimeUnitTestToolkit for Unity
Unity Unit Test is not supported runtime unit test. IL2CPP or other machine specific issue is a serious issue. We should check it.
get .unitypackage
on releases page.
Open RuntimeUnitTestToolkit/UnitTest.scene
, it is test explorer.
Write first test class.
// make unit test on plain C# class
public class SampleGroup
{
// all public methods are automatically registered in test group
public void SumTest()
{
var x = int.Parse("100");
var y = int.Parse("200");
// using RuntimeUnitTestToolkit;
// 'Is' is Assertion method, same as Assert(actual, expected)
(x + y).Is(300);
}
// return type 'IEnumerator' is marked as async test method
public IEnumerator AsyncTest()
{
var testObject = new GameObject("Test");
// wait asynchronous coroutine(UniRx coroutine runnner)
yield return MainThreadDispatcher.StartCoroutine(MoveToRight(testObject));
// assrtion
testObject.transform.position.x.Is(60);
GameObject.Destroy(testObject);
}
IEnumerator MoveToRight(GameObject o)
{
for (int i = 0; i < 60; i++)
{
var p = o.transform.position;
p.x += 1;
o.transform.position = p;
yield return null;
}
}
}
finally register test classes on test manager
public static class UnitTestLoader
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Register()
{
// setup created test class to RegisterAllMethods<T>
UnitTest.RegisterAllMethods<SampleGroup>();
// and add other classes
}
}
play UnitTest scene
Standard API(static Assert methods)
- Assert.AreEqual
- Assert.AreNotEqual
- Assert.IsTrue
- Assert.IsNull
- Assert.IsNotNull
- Assert.Fail
- Assert.AreSame
- Assert.AreNotSame
- Assert.IsInstanceOfType
- Assert.IsNotInstanceOfType
- Assert.Catch
- Assert.Throws
- Assert.DoesNotThrow
- CollectionAssert.AreEqual
- CollectionAssert.AreNotEqual
Extension API(extension methods of object)
These api makes standard api to fluent sytnax
AssertAreEqual(actual, expected)
-> actual.Is(expected)
.
- Is
- IsCollection
- IsNot
- IsNotCollection
- IsEmpty
- IsNull
- IsNotNull
- IsTrue
- IsFalse
- IsSampeReferenceAs
- IsNotSampeReferenceAs
- IsInstanceOf
- IsNotInstanceOf
These API is port of neuecc/ChainingAssertion
UniRx helps Unit test easily. event as IObservable<T>
, ObserveOnEveryValueChanged
, ObservableTriggers
can watch test target's state from outer environment.
public IEnumerator WithUniRxTestA()
{
// subscribe event callback
var subscription = obj.SomeEventAsObservable().First().ToYieldInstruction();
// raise event
obj.RaiseEventSomething();
// check event raise complete
yield return subscription;
subscription.Result.Is();
}
public IEnumerator UniRxTestB()
{
// monitor value changed
var subscription = obj.ObserveEveryValueChanged(x => x.someValue).Skip(1).First().ToYieldInstruction();
// do something
obj.DoSomething();
// wait complete
yield return subscription;
subscription.Result.Is();
}
Yoshifumi Kawai(a.k.a. neuecc) is a software developer in Japan.
He is the Director/CTO at Grani, Inc.
He is awarding Microsoft MVP for Visual C# since 2011.
He is known as the creator of UniRx(Reactive Extensions for Unity)
Blog: https://medium.com/@neuecc (English)
Blog: http://neue.cc/ (Japanese)
Twitter: https://twitter.com/neuecc (Japanese)
This library is under the MIT License.