{!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} 4. Reach Wand Test | Test Framework | 1.3.9
docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    4. Reach Wand Test

    Learning Objectives

    Perform Assertions on your character position and behavior.

    Exercise

    1. Go back to the previous MovementTest.cs file.
    2. Write a MainScene\_CharacterReachesWand test that makes your character move right, and checks if it reaches the wand location.

    Hints

    • Look for Altar and Sara Variant game objects in your scene. You are interested in measuring the X position of your Transform objects.
    • Wand location X position is equal float of 21.080. Main Character X position is dynamic and it changes whenever it moves.
    • Consider setting a timeout that makes the test fail if the Wand is not reached.

    Solution

    using System.Collections;
    using NUnit.Framework;
    using UnityEngine;
    using UnityEngine.TestTools;
    using UnityEngine.SceneManagement;
    
    public class MovementTest
    {
        private Transform _characterTransform;
        private float _testTimeout = 25.0f;
        private float _wandLocation = 21.080f;
    
        [UnityTest]
        public IEnumerator MainScene_CharacterReachesWand()
        {
          SceneManager.LoadScene("Assets/Scenes/Main.unity", LoadSceneMode.Single);
          yield return waitForSceneLoad();
    
          var elapsedTime = 0.0f;
          yield return GoRight();
          while (GetCurrentCharacterPosition() <= _wandLocation)
          {
              yield return null;
              elapsedTime += Time.deltaTime;
              if (elapsedTime > _testTimeout)
              {
                Assert.Fail($"Character did not reach location position in {_testTimeout} seconds.");
              }
          }
        }
    
        private float GetCurrentCharacterPosition()
        {
          // Get Main character's Transform which is used to manipulate position.
          if (_characterTransform == null)
          {
              _characterTransform = GameObject.Find("Sara Variant").transform;
          }
    
          return _characterTransform.position.x;
        }
    
        private IEnumerator GoRight()
        {
          TestInputControl.MoveLeft = false;
          yield return null;
          TestInputControl.MoveRight = true;
        }
    
        private IEnumerator waitForSceneLoad()
        {
            while (SceneManager.GetActiveScene().buildIndex > 0)
            {
                yield return null;
            }
        }
    }
    
    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)