Unity Test Runner是集成在Unity里面的单元测试,基于NUnit单元测试。使用方法:
在Unity菜单Window->TestRunner打开窗口。
可以看到分为两种模式,一种是PlayMode,一种是EditMode。
PlayMode是一种运行时测试的,就是你运行测试的时候会自动播放游戏的。如果说需要测试MonoBehaviour的脚本,一定得是PlayMode的。
EditMode就是在编辑器当中就可以运行的了,一些普通的脚本啊,一些扩展编辑器的脚本啊等等。
点击Create Test Assembly Folder,创建一个测试的程序集文件夹,会有一个Assembly Definition文件在文件夹里面,可以指定平台。
随后进入到文件夹里面,Create Test Script in current folder就可以用了,点击它,创建一个测试脚本。
using UnityEngine.TestTools; using NUnit.Framework; using System.Collections; public class NewTestScript { [Test] public void NewTestScriptSimplePasses() { // Use the Assert class to test conditions. } // A UnityTest behaves like a coroutine in PlayMode // and allows you to yield null to skip a frame in EditMode [UnityTest] public IEnumerator NewTestScriptWithEnumeratorPasses() { // Use the Assert class to test conditions. // yield to skip a frame yield return null; } }
可以发现使用到了UnityEngine.TestTools和NUnit.Framework。
这里面有两种特性,Test和UnityTest指定的不同的测试方法,在Unity Test Runner中可以看到了
双击就可以测试对应里面的测试代码了。