Unity Test Runner 在 Edit 模式和 Play 模式下以及在目标平台(如独立平台、Android 或 iOS)上测试您的代码。
本页面上的文档将讨论在 Unity Test Runner 中编写和执行测试,并假设您了解脚本和 Unity Test Runner。
Unity 以 XML 格式提供测试结果。有关更多信息,请参阅有关 NUnit 文档关于 XML 格式测试结果的说明。
UnityTestAttribute 要求您返回 IEnumerator
。在 Play 模式下,以协程的形式执行测试。在 Edit 模式下,可从测试中产生 null
,这将跳过当前帧。
__注意:__WebGL 和 WSA 平台不支持 UnityTestAttribute
。
[Test]
public void GameObject_CreatedWithGiven_WillHaveTheName()
{
var go = new GameObject("MyGameObject");
Assert.AreEqual("MyGameObject", go.name);
}
[UnityTest]
public IEnumerator GameObject_WithRigidBody_WillBeAffectedByPhysics()
{
var go = new GameObject();
go.AddComponent<Rigidbody>();
var originalPosition = go.transform.position.y;
yield return new WaitForFixedUpdate();
Assert.AreNotEqual(originalPosition, go.transform.position.y);
}
[UnityTest]
public IEnumerator EditorUtility_WhenExecuted_ReturnsSuccess()
{
var utility = RunEditorUtilityInTheBackgroud();
while (utility.isRunning)
{
yield return null;
}
Assert.IsTrue(utility.isSuccess);
}
使用 UnityPlatformAttribute 可根据执行平台来过滤测试。行为类似于 NUnit PlatformAttribute。
[Test]
[UnityPlatform (RuntimePlatform.WindowsPlayer)]
public void TestMethod1()
{
Assert.AreEqual(Application.platform, RuntimePlatform.WindowsPlayer);
}
[Test]
[UnityPlatform(exclude = new[] {RuntimePlatform.WindowsEditor })]
public void TestMethod2()
{
Assert.AreNotEqual(Application.platform, RuntimePlatform.WindowsEditor);
}
要仅在给定平台上执行 Editor 测试,还可以使用 UnityPlatform
。
如果需要在测试开始之前执行任何额外设置,请使用 PrebuildSetupAttribute。为此,请指定用于实现 IPrebuildSetup 接口的类类型。如果需要运行整个类的设置代码(例如,如果要在测试开始之前执行某些代码,如资源准备或特定测试所需的设置),请在用于测试的类中实现 IPrebuildSetup
接口。
public class TestsWithPrebuildStep : IPrebuildSetup
{
public void Setup()
{
// 在执行测试之前运行此代码
}
[Test]
//可跳过 PrebuildSetupAttribute,因为它已在同一个类中实现
[PrebuildSetup(typeof(TestsWithPrebuildStep))]
public void Test()
{
(...)
}
}
在进入 Play 模式或构建播放器之前执行 IPrebuildSetup
代码。安装程序可使用 UnityEditor 命名空间及其函数,但为了避免编译错误,必须将其放在 Editor 文件夹中,或使用 #if UNITY_EDITOR
指令对其进行保护。
如果 Unity 记录除常规日志或警告消息之外的消息,表明测试失败。使用 LogAssert 类可在日志中发出一条预期的消息,确保在 Unity 记录该消息时测试不会失败。
如果未显示预期消息,或者 Unity 未记录任何常规日志或警告消息,测试也会报告为失败。
[Test]
public void LogAssertExample()
{
//预期会出现一条常规日志消息
LogAssert.Expect(LogType.Log, "Log message");
//预期会出现一条日志消息,如果没有以下行,
//测试将失败
Debug.Log("Log message");
//输出错误日志
Debug.LogError("Error message");
//如果不做错误日志的预期,测试将失败
LogAssert.Expect(LogType.Error, "Error message");
}
MonoBehaviourTest 是一个协程,也是编写 MonoBehaviour 测试的 helper 函数。从 UnityTest 属性中生成 MonoBehaviourTest
即可实例化指定的 MonoBehaviour 并等待其完成执行。实现 IMonoBehaviourTest 接口可指示测试何时完成。
[UnityTest]
public IEnumerator MonoBehaviourTest_Works()
{
yield return new MonoBehaviourTest<MyMonoBehaviourTest>();
}
public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
{
private int frameCount;
public bool IsTestFinished
{
get { return frameCount > 10; }
}
void Update()
{
frameCount++;
}
}
在 Play 模式下,可在特定平台上运行测试。目标平台始终是在 Build Settings(菜单:__File__ > Build Settings__)中选择的当前平台。单击 Run all in the player__ 即可在当前激活的目标平台上构建并运行测试。
请注意,当前的平台会显示在按钮上的括号中。例如,在下面的截屏中,按钮显示为 Run all in player (StandaloneWindows),因为当前平台是 Windows。
测试完成后,测试结果将立即显示在构建中。
要将测试结果从平台传送到运行测试的 Editor,两者需要在同一网络上。在平台上运行的应用程序将回报测试结果,显示执行的测试,并关闭。
请注意,有些平台不支持使用 Application.Quit 关闭应用程序。这些平台在报告测试结果后继续运行应用程序。
如果 Unity 无法实例化连接,您可以直观地看到测试在正在运行的应用程序中成功。请注意,在此状态下,使用参数在平台上运行测试不会提供 XML 测试结果。
runTests
- 在项目中执行测试。
testPlatform
- 要运行测试的平台。
可用平台:
playmode
和 editmode
。请注意,如果未指定,则默认情况下,测试在 editmode
中运行。
平台/类型约定来自 BuildTarget 枚举。经过测试且官方支持的平台:
testResults
- 指示 Unity 应保存结果文件的路径。默认情况下,Unity 将其保存在项目的根文件夹中。
以下示例可在 Windows 上显示一个命令行参数。具体代码行可能因操作系统而异。
>Unity.exe -runTests -projectPath PATH_TO_YOUR_PROJECT -testResults C:\temp\results.xml -testPlatform editmode
__提示:__在 Windows 上,为了读取已执行命令的结果代码,请运行以下命令:
start /WAIT Unity.exe ARGUMENT_LIST
UnityEngine.TestTools.Utils 命名空间包含实用程序类以便使用 NUnit 约束来比较 Vector2、Vector3、Vector4、Quaternion、Color 和 float
类型。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.