Testing UI behaviour is not an easy task. But there are multiple ways to do some useful tests without much effort.
Clicking UI
Usually as simple as finding references to UI. Might be harder at times. But usually finding by name should be enough. And if your project is big enough that finding by name is not an option or if the naming is bound to change. Fetch the references from any other type that you hold in the scene or use tags.
Once you have the reference to your UI item. You can execute Button.onClick.Invoke() and equivalent for sliders/inputfields.
1 2 3 4 5 6 7 8 9 10 11
[UnityTest] public IEnumerator UserInterface_ClickButton_DisablesObject() { SceneManager.LoadScene("UI_Button"); yieldreturnnull;
Assert.IsFalse(btn.gameObject.activeInHierarchy, "The button should deactivate the gameobject it is attached to."); }
Verifying element visibility
In my project I ran into problems where UI would go out of view for different resolutions. And in my particular example All my canvases should have been visible on screen if enabled.
First we make an extension method where we could test if one RectTransform contains another one. Methods that unity provides don’t take anchors and pivots into account.
staticpublic Rect GetRectRelativeToScreen(this RectTransform rectTransform) { Vector3[] corners = new Vector3[4]; rectTransform.GetWorldCorners(corners); Vector2 size = new Vector2(corners[3].x - corners[0].x, corners[1].y - corners[0].y); returnnew Rect(corners[0], size);; }
publicstaticboolContains(this RectTransform rt, RectTransform rectTransform) { Rect parent = rt.GetRectRelativeToScreen(); Rect child = rectTransform.GetRectRelativeToScreen(); return Fits(parent, child); } }
Now going to the test…. We can’t change the resolution of game view by script so it should exclude editor. Then we provide resolution list from a static function. Fetch all canvases and go through all items. And done!
foreach (var canvas in GameObject.FindObjectsOfType(typeof(Canvas)) as Canvas[]) { RectTransform canvasTransform = canvas.GetComponent<RectTransform>(); foreach (RectTransform rectTransform in canvas.transform) Assert.IsTrue(canvasTransform.Contains(rectTransform), string.Format("UI element named {0} is out of bounds.", rectTransform.name)); } }
publicstatic Vector2Int[] SupportedResolutions() { returnnew [] { new Vector2Int(1440,900), new Vector2Int(1920,1080), new Vector2Int(1280,720) }; }
Hope you found something useful that you could apply in your adventures with Unity and its test runner.