UnityTestTool集成测试
在网上发现了一篇关于 Unity Test Tool 集成测试的文章,有兴趣的同学可以深入研究一下。
测试前准备:创建场景
首先,创建一个简单的场景,在该场景中,一个立方体进行自由落体运动,最终落在地形上。
开始测试
- 创建测试对象及脚本
- 选择菜单栏中的
Unity Test Tools->Integration。 - 点击加号,新建一个
New Test。在New Test之下创建一个GameObject。 - 创建一个名为
TestScript的脚本,并将其绑定到该GameObject上。以下是TestScript脚本的代码:using UnityEngine; using System.Collections; using System;
- 选择菜单栏中的
public class TestScript : MonoBehaviour { // Use this for initialization void Start () { Debug.Log ("StartTime=" + Time.time); IntegrationTest.Pass (); }
void Update(){ Debug.Log ("Time=" + Time.time); } }
2. **运行测试**
点击测试工具的播放键(注意不是游戏的播放键)。此时,可以清晰地看到游戏运行了一帧,并且测试通过。
## 加入异常处理
### 配置异常处理设置
首先,在 `New Test` 的属性中进行勾选操作,这样在编写抛异常语句时不会报错。
### 修改测试脚本
修改 `TestScript.cs` 脚本,具体代码如下:
using UnityEngine; using System.Collections; using System;
public class TestScript : MonoBehaviour { // Use this for initialization void Start () { Debug.Log ("StartTime=" + Time.time); // 注销掉 IntegrationTest.Pass(); // IntegrationTest.Pass (); }
int i = 0; void Update(){ Debug.Log ("Time=" + Time.time); if (i == 3) { throw new Exception ("wrong"); } i++; } }
注意要注销掉 `IntegrationTest.Pass();`,你可以对比注销和不注销的运行效果,从而得出 `IntegrationTest.Pass();` 的作用。
## 测试完成
运行测试后,即可完成整个测试流程。
通过以上步骤,我们完成了 Unity Test Tool 的集成测试,并对异常处理进行了实践。