docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class MonoBehaviourTest<T>

    This is a wrapper that allows running tests on MonoBehaviour scripts. Inherits from CustomYieldInstruction MonoBehaviourTest is a coroutine and a helper for writing MonoBehaviour tests. Yield a MonoBehaviourTest when using the UnityTest attribute to instantiate the MonoBehaviour you wish to test and wait for it to finish running. Implement the IMonoBehaviourTest interface on the MonoBehaviour to state when the test completes.

    Inheritance
    object
    CustomYieldInstruction
    MonoBehaviourTest<T>
    Implements
    IEnumerator
    Inherited Members
    CustomYieldInstruction.MoveNext()
    CustomYieldInstruction.Reset()
    CustomYieldInstruction.Current
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: UnityEngine.TestTools
    Assembly: UnityEngine.TestRunner.dll
    Syntax
    public class MonoBehaviourTest<T> : CustomYieldInstruction, IEnumerator where T : MonoBehaviour, IMonoBehaviourTest
    Type Parameters
    Name Description
    T

    A MonoBehaviour component created for the test and attached to the tests GameObject.

    Constructors

    MonoBehaviourTest(bool)

    Initializes and returns an instance of MonoBehaviourTest.

    Declaration
    public MonoBehaviourTest(bool dontDestroyOnLoad = true)
    Parameters
    Type Name Description
    bool dontDestroyOnLoad
    Examples
    [UnityTest]
    public IEnumerator MonoBehaviourTest_Works()
    {
        yield return new MonoBehaviourTest<MyMonoBehaviourTest>();
    }
    
    public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
    {
        private int frameCount;
        public bool IsTestFinished
        {
            get { return frameCount > 10; }
        }
    
        void Update()
        {
            frameCount++;
        }
    }

    Properties

    component

    A MonoBehaviour component created for the test and attached to the test's GameObject.

    Declaration
    public T component { get; }
    Property Value
    Type Description
    T

    gameObject

    A GameObject created as a container for the test component.

    Declaration
    public GameObject gameObject { get; }
    Property Value
    Type Description
    GameObject

    keepWaiting

    (Inherited) Returns `true`` if the test is not finished yet, which keeps the coroutine suspended

    Declaration
    public override bool keepWaiting { get; }
    Property Value
    Type Description
    bool
    Overrides
    CustomYieldInstruction.keepWaiting

    Implements

    IEnumerator
    In This Article
    Back to top
    Copyright © 2024 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)