Test UI with Editor window instances
If your tests require an actual EditorWindow instance, use EditorWindowUITestFixture to create tests that spawn and manage an EditorWindow instance. With this fixture, an Editor panel attached to a real EditorWindow hosts your UI, so the UI is rendered and visible on the screen during testing.
Note
To test your UXML or your custom control, use UITestFixture.
The following example shows how to set up your test class to use EditorWindowUITestFixture:
public class BasicEditorWindowExample : EditorWindowUITestFixture<UITestFrameworkDocSampleWindow>
{
[Test]
public void EditorWindowTest()
{
// Ensure the window's UI is up to date.
simulate.FrameUpdate();
// Use the rootVisualElement property to query for elements
// within the window created by the test fixture.
Button button = rootVisualElement.Q<Button>("MyButton");
Assert.That(button, Is.Not.Null);
// Test steps.
// ...
}
}
Limitations of the FrameUpdate function for Editor windows
The FrameUpdate() method executes the UI Toolkit update loop.
However, if your Editor window relies on custom logic defined in an Update() method or is tied to the Editor update loop, tests must either call the update methods directly or wait for them to execute normally via coroutines.
The following example shows how to write a test that validates the custom logic of a window's Update function:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class EditorWindowWithCustomUpdate : EditorWindow
{
[MenuItem("Window/UI Toolkit/UI Test Framework/Editor Window With Custom Update")]
public static void ShowExample()
{
EditorWindowWithCustomUpdate wnd = GetWindow<EditorWindowWithCustomUpdate>();
wnd.titleContent = new GUIContent("EditorWindowWithCustomUpdate");
}
int m_ButtonClickedCount = 0;
public Button Button;
public Label ButtonClickedCountLabel;
string m_Text = "Times button was clicked: ";
public void CreateGUI()
{
m_ButtonClickedCount = 0;
VisualElement root = rootVisualElement;
Button = new Button() { text = "Button not clicked" };
Button.clicked += () =>
{
Button.text = "Button was clicked!"; m_ButtonClickedCount++;
};
root.Add(Button);
ButtonClickedCountLabel = new Label()
{
text = m_Text + m_ButtonClickedCount
};
root.Add(ButtonClickedCountLabel);
}
public void Update()
{
ButtonClickedCountLabel.text = m_Text + m_ButtonClickedCount;
}
}
using NUnit.Framework;
using UnityEditor.UIElements.TestFramework;
public class EditorWindowWithCustomUpdateTest : EditorWindowUITestFixture<EditorWindowWithCustomUpdate>
{
[Test]
public void ExplicitlyCallUpdateFunction_ForWindowWithCustomUpdate()
{
simulate.FrameUpdate();
simulate.Click(window.Button);
simulate.FrameUpdate();
Assert.That(window.Button.text, Is.EqualTo("Button was clicked!"));
// The label's text won't have updated since its logic
// is tied to the window's update function.
Assert.That(window.ButtonClickedCountLabel.text,
Is.EqualTo("Times button was clicked: 0"));
// Call the window's update function directly,
// or wait for actual frames using coroutines.
window.Update();
Assert.That(window.ButtonClickedCountLabel.text,
Is.EqualTo("Times button was clicked: 1"));
}
}