{!See https://docs.google.com/document/d/1takg_GmIBBKKTj-GHZCwzxohpQz7Bhekivkk72kYMtE/edit for reference implementation of OneTrust, dataLayer and GTM} {!OneTrust Cookies Consent} {!OneTrust Cookies Consent end} {!dataLayer initialization push} {!dataLayer initialization push end} {!Google Tag Manager} {!Google Tag Manager end} 9. Using the UnityTest Attribute | Test Framework | 1.3.9
docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    9. Using the UnityTest Attribute

    Learning objectives

    This section will introduce you to the custom [UnityTest] Attribute, which allows for creating tests that run over multiple frames.

    Intro and motivation

    An important extension to the Nunit framework that we've made is introducing the [UnityTest] attribute. The attribute allows for creating tests that can yield and resume running after a certain condition. Therefore the test must have the return type of IEnumerator. You can then yield back a yield instruction or null, like so:

    [UnityTest]
    public IEnumerator MyTest()
    {
     DoSomething();
     // Skip 1 frame.
     yield return null;
     DoSomethingElse();
    }
    

    In the snippet above we call the DoSomething method, then skip one frame before calling the DoSomethingElse method.

    For more information on the yield keyword in C#, see the Microsoft documentation.

    Exercise

    In the sample 9_UnityTestAttribute you will find a Play Mode test assembly set up with one Play Mode test in it. The PlayMode test does not have a body yet, but there is a function called PrepareCube() which will set up a cube with some physics applied.

    The task is to initialize the cube and then verify that it has moved after one frame has passed.

    Solution

    The full solution is available in the 9_UnityTestAttribute_Solution sample.

    [UnityTest]
    public IEnumerator CubeMovesDown()
    {
     var cubeUnderTest = PrepareCube();
     var initialPosition = cubeUnderTest.transform.position;
    
     yield return null;
    
     Assert.That(cubeUnderTest.transform.position, Is.Not.EqualTo(initialPosition));
    }
    

    Further reading and resources

    UTF documentation regarding UnityTest attribute

    In This Article
    Back to top
    Copyright © 2023 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)