Last active
June 22, 2020 00:57
-
-
Save colinbellino/41bdf47e0060fde7fdc149554d4c94ff to your computer and use it in GitHub Desktop.
Unity Integration Tests Example. (full project: https://github.com/colinbellino/UnityIntegrationTestsExample)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using NUnit.Framework; | |
using UnityEngine; | |
using UnityEngine.TestTools; | |
using Zenject; | |
public class SampleTest : SceneTestFixture | |
{ | |
[UnityTest] | |
public IEnumerator MovesAroundTheLevel() | |
{ | |
// Inject out "test" level data. | |
var levelData = new LevelData | |
{ | |
PlayerPosition = new Vector2Int(8, -1), | |
Board = new int[,] { | |
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | |
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
{ 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 }, | |
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, | |
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1 }, | |
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, | |
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, | |
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, | |
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | |
}, | |
}; | |
StaticContext.Container.BindInstance(levelData); | |
yield return LoadScene("SampleScene"); | |
// Get the Commander that was injected into the scene. | |
var commander = SceneContainer.Resolve<Commander>(); | |
commander.Execute(new MovePlayer(new Vector2Int(-1, 0))); | |
// ... More commands here. | |
// Wait one frame so the player actually moved. | |
yield return null; | |
// Check that the player is were we expect them to be. | |
var playerPosition = GameObject.Find("Player").transform.position; | |
Assert.AreEqual(playerPosition, new Vector3(7, 0)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment