环境
Unity 2020.3.3f1
流程
1. 创建一个存放 单元测试程序集 的目录
2. 打开 Test Runner 窗口
- 在Unity编辑器中,选择 Window -> General -> Test Runner
3. 选择单元测试模式
4. 创建单元测试程序集
- 确定现在选择的是存放目录(我这里是Tests)
- 按下 Test Runner 窗口的 Create (PlayMode|EditMode) Test Assembly Folder
5. 创建测试脚本
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
public class MyTestScript
{
// A Test behaves as an ordinary method
[Test]
public void MyTestScriptSimplePasses()
{
// Use the Assert class to test conditions
Debug.Log(nameof(MyTestScriptSimplePasses));
}
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
// `yield return null;` to skip a frame.
[UnityTest]
public IEnumerator MyTestScriptWithEnumeratorPasses()
{
// Use the Assert class to test conditions.
// Use yield to skip a frame.
yield return null;
Debug.Log(nameof(MyTestScriptWithEnumeratorPasses));
}
}
6. 运行测试
- 在 Test Runner 窗口运行测试
- 也可以使用Rider的单元测试(Rider)