Unity3D-UnityTestTool单元测试(续)测试进行中的游戏

从上周开始阅读,UnityTestTool的英文文档。


在Docs下有英语和日语的说明文档。实际上,就是结合Examples文件夹下面的几个例子讲了讲这个工具如何运用。

例子简单粗暴,改天有空的话,我可以贴一贴自己的阅读心得。

但是我最需要的unit test讲的很少。

好吧。

只好借用这篇文章结合UnityTestTool来进行单元测试。


ok,先贴一下链接,

http://gamerboom.com/archives/49346

之所以要重写一些,是因为有些地方你直接贴代码的话会报错,我简单的处理一下错误,并运行出来结果。

ok,在工程下新建Editor文件夹(用于放测试代码)

先写一个工具类

ScriptInstantiator.cs

using UnityEngine;
using System.Collections;
using System;
using Microsoft;
using System.Collections.Generic;
using System.Reflection;

public class ScriptInstantiator  {
	private static List<GameObject> GameObjects { get; set; }

	public ScriptInstantiator()
	{
		GameObjects = new List<GameObject>();
	}

	public static T InstantiateScript<T>(String prefabsName) where T : MonoBehaviour
	{
		GameObject gameObject;
		//object prefab = Resources.Load("Prefabs/""Resources/" + typeof(T).Name);
		object prefab = Resources.Load(prefabsName);
		// If there is no prefab with the same name, just use an empty object

		//
		if (prefab == null)
		{
			Debug.Log ("prefab==null");
			gameObject = new GameObject();
		}
		else
		{
			Debug.Log ("prefab!=null");
			//gameObject = GameObject.Instantiate(Resources.Load("Prefabs/"
			//	+ typeof(T).Name)) as GameObject;
			gameObject = GameObject.Instantiate (Resources.Load (prefabsName))as GameObject;
		}

		//gameObject.name = typeof(T).Name + " (Test)";
		gameObject.name = prefabsName + "(Test)";
		// Prefabs should already have the component
		T inst = gameObject.GetComponent<T>();

		if (inst == null)
		{
			inst = gameObject.AddComponent<T>();
		}

		// Call the start method to initialize the object

		//

		MethodInfo startMethod = typeof(T).GetMethod("Start");
		if (startMethod != null)
		{
			Debug.Log ("startMethod!=null");
			startMethod.Invoke(inst, null);
		}else
			Debug.Log ("startMethod==null");
		GameObjects.Add(gameObject);
		return inst;
	}

	public void CleanUp()
	{
		foreach (GameObject gameObject in GameObjects) {
			// Destroy() does not work in edit mode
			GameObject.DestroyImmediate (gameObject);
		}
		GameObjects.Clear();
	}

}

ok,这个工具类的含义是寻找 一个脚本类,传入此脚本所绑定的prefab名,函数自动运行start函数,并返回此脚本类(此时已实例化为绑定在对象身上的脚本),就可以进行具体测试了。

因为,单元测试不是集成测试,不需要运行整个工程,只需要运行相应的类或方法就可以。所以,我们只需要获取实例化后的脚本,而不需要整个GameObject对象。注意理清关系。

开始写测试脚本

HeroControllerTest.cs

using System;
using UnityEngine;
using System.Collections;
using NUnit.Framework;

namespace BaojieTesting
{
	[TestFixture]
	public class HeroControllerTest
	{  
		public HeroControllerTest(){
		}

		[Test]  
		public void Test(){
			HeroController source=ScriptInstantiator.InstantiateScript<HeroController>("CaddyPrefab");

		}


	}  
}
我们 要测的是,HeroController.cs

ok,可以看到已经能进入start函数。

(关于MethodInfo的使用 http://sunct.iteye.com/blog/745898


以后持续再写吧,下班!








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值