[Unity] - PlayMode test problems

edited in General
Hey all,

I' m busy going through my project adding some tests and finding some really annoying things with the PlayMode tests and was wondering if anyone has any workarounds.

1. You can't run the tests on a predefined scene. To get around this I have to instantiate everything in the` SetUp` method and clean it all out in the `TearDown`. Its not the worse thing in the world, but it is kinda annoying getting things placed correctly.

private GameObject _accomplice;
	private GameObject _door;
	private GameObject _defaults;
	private InteractionUIScript _ui;

	[SetUp]
	public void SetUp()
	{
		_accomplice = MonoBehaviour.Instantiate(Resources.Load<GameObject>("Prefabs/AccomplicePrefab"));
		_accomplice.transform.position = new Vector2(0f, 0f);

		_door = MonoBehaviour.Instantiate(Resources.Load<GameObject>("Prefabs/DoorPrefab"));
		_door.transform.position = new Vector2(-0.8f, -0.7f);

		_defaults = MonoBehaviour.Instantiate(Resources.Load<GameObject>("Prefabs/SceneDefaults"));
		_ui = GameObject.FindObjectOfType<InteractionUIScript>();
	}

	[TearDown]
	public void TearDown()
	{
		GameObject.Destroy(_defaults);
		GameObject.Destroy(_accomplice);
		GameObject.Destroy(_door);
		_defaults = null;
		_accomplice = null;
		_door = null;
		_ui = null;
	}


2. If I don't have a small delay between tests, its as if the TearDown/SetUp process hasnt completed and my subsequent tests fail. Note the `WaitForSeconds`, without it, `WhenAccompliceIsNextToDoor_TheUiShows2` will fail, even though the test is identical.

[UnityTest]
	public IEnumerator WhenAccompliceIsNextToDoor_TheUiShows()
	{
		yield return new WaitForSeconds(0.1f);
		Assert.IsTrue(_ui.IsOpen);
	}

	[UnityTest]
	public IEnumerator WhenAccompliceIsNextToDoor_TheUiShows2()
	{
		yield return new WaitForSeconds(0.1f);
		Assert.IsTrue(_ui.IsOpen);
	}


3. There doesn't seem to be a way to send inputs. I can fudge this by calling my own `HandleInput` method, but it feels kinda wrong.


Other than these nuances, I really like that they have this built in :)

Comments

Sign In or Register to comment.