Create your first UI test
Use this simple example to get started with the UI Test Framework. This example demonstrates how to set up a simple UI, create a test using the test fixtures, and run a test that simulates a button click to verify its behavior.
Create a simple UI for testing
Create a simple Editor window with a button to test. When the button is clicked, its text changes to Button was clicked!.
Create a Unity project and install the UI Test Framework package.
In the Project window, create a folder named
Editor(if it doesn't already exist).In the
Editorfolder, create a new C# script namedSimpleEditorWindow.cswith the following content:using UnityEditor; using UnityEngine; using UnityEngine.UIElements; public class SimpleEditorWindow : EditorWindow { [MenuItem("Window/UI Toolkit/UI Test Framework/Simple Editor Window")] public static void ShowExample() { SimpleEditorWindow wnd = GetWindow<SimpleEditorWindow>(); wnd.titleContent = new GUIContent("SimpleEditorWindow"); } public void CreateGUI() { VisualElement root = rootVisualElement; Button button = new Button() { text = "Button not clicked" }; button.clicked += () => { button.text = "Button was clicked!"; }; root.Add(button); } }
Create the assembly definition
Create an assembly definition for the SimpleEditorWindow script to allow the test class to reference it. Also create an assembly definition for the test fixture itself and reference the necessary test framework assemblies.
In the
Editorfolder, right-click and select Create > Scripting > Assembly Definition to create an assembly definition file namedSimpleEditorWindow.Editor.asmdefwith the following content:{ "name": "SimpleEditorWindow.Editor", "includePlatforms": [ "Editor" ] }Inside the
Editorfolder, create a folder namedTests.Inside the
Testsfolder, right-click and select Create > Scripting > Assembly Definition to create an assembly definition file namedSimpleEditorWindow.Tests.Editor.asmdefwith the following content:{ "name": "SimpleEditorWindow.Tests.Editor", "references": [ "UnityEngine.TestRunner", "UnityEditor.TestRunner", "Unity.UI.TestFramework.Editor", "Unity.UI.TestFramework.Runtime", "SimpleEditorWindow.Editor" ], "includePlatforms": [ "Editor" ] }
Create the test script and run the tests
Create a test class that inherits from the EditorWindowUITestFixture because the test requires an actual EditorWindow instance. For more information, refer to Introduction to test fixtures. The test simulates a button click, and verifies that the button text changes as expected.
Inside the
Editor/Testsfolder, create a C# script namedSimpleEditorWindowTest.cswith the following content:using NUnit.Framework; using UnityEditor.UIElements.TestFramework; using UnityEngine.UIElements; public class SimpleEditorWindowTest : EditorWindowUITestFixture<SimpleEditorWindow> { [Test] public void ClickButtonTest() { simulate.FrameUpdate(); Button button = rootVisualElement.Q<Button>(); Assume.That(button, Is.Not.Null); Assume.That(button.text, Is.EqualTo("Button not clicked")); simulate.Click(button); simulate.FrameUpdate(); Assert.That(button.text, Is.EqualTo("Button was clicked!")); } }From the menu, select Window > General > Test Runner.
In the Test Runner window, select the EditMode tab.
Select
SimpleEditorWindow.Tests.Editor.dll.Select Run to execute your tests.