Skip to content

Unity unit test framework focused on run play time and actual machine

License

Notifications You must be signed in to change notification settings

leechen2020/RuntimeUnitTestToolkit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

RuntimeUnitTestToolkit

Unity unit test framework focused on run play time and actual machine

image

Why needs this?

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.

How to use?

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

image

Assertion API

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

with UniRx

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();
}

Author Info

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)

License

This library is under the MIT License.

About

Unity unit test framework focused on run play time and actual machine

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%