Test UI in runtime
Use RuntimeUITestFixture to create tests that run in Play mode.
This fixture allows you to load and test an existing Scene containing your UI.
The following example shows how to set up your test class to use RuntimeUITestFixture in a Play mode test assembly:
// This class is contained in a runtime test assembly.
public class BasicRuntimeExampleClass : RuntimeUITestFixture
{
[UnityOneTimeSetUp]
public IEnumerator UnityOneTimeSetUp()
{
// Load the scene and yield a player
// frame to ensure it is up to date.
yield return SceneManager.LoadSceneAsync("TestScene", LoadSceneMode.Single);
yield return null;
}
[Test]
public void BasicRuntimeExampleTest()
{
// Fetch the UIDocument from the scene.
UIDocument uiDocument = FetchUIDocument();
// and pass it to SetUIContent
// to hook it up for simulation.
SetUIContent(uiDocument);
simulate.FrameUpdate();
Button button = rootVisualElement.Q<Button>("MyButton");
Assert.That(button, Is.Not.Null);
// Test steps.
// ...
}
public UIDocument FetchUIDocument()
{
// Locate the gameObject that contains your UIDocument content.
var gameObject = GameObject.Find("UIDocument");
// Get the UIDocument component.
UIDocument uiDocument = gameObject.GetComponent<UIDocument>();
uiDocument.panelSettings = Resources.Load<PanelSettings>("UITestFrameworkDocSamplePanelSettings");
uiDocument.visualTreeAsset = Resources.Load<VisualTreeAsset>("UITestFrameworkDocSample");
return uiDocument;
}
}