Perform Assertions on your character position and behavior.
MovementTest.cs
file.MainScene\_CharacterReachesWand
test that makes your character move right, and checks if it reaches the wand location.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;
}
}
}