我试图在C#中为需要在Unity3D中运行的脚本编写单元测试,以后。我的C#脚本在Unity项目的文件夹"资产/脚本"中。
我正在使用 UNITY版本2019.1.0f2 带有 Visual Studio Community for Mac v8.0.5(构建9(,在 Mac> Mac OS X版本10.14.4。
Unity具有我使用的测试跑步者窗口:Window > General > Test runner
。在该测试跑步者窗口内,我单击了"播放模式",然后在"创建PlayMode Test Assembly文件夹"上单击。Unity为我创建了一个称为"测试"的文件夹,我应该在其中进行测试。到目前为止,一切都很好。
现在是问题:
我的测试代码看不到该测试文件夹的父文件夹中的测试类!
这是文件夹层次结构:
Assets
Scripts
MyFirstScript.cs
Tests
MyTestScript.cs
Tests.asmdef
当我在mytestscript.cs上双击时,Visual Studio会打开解决方案,源文件确定。现在,我正在尝试写
var x = new MyFirstScript();
编译器抱怨"找不到类型或名称空间名称'myfirstscript'。
似乎脚本文件夹中的类似乎是完全未知的,在测试文件夹中的类中。
这是myfirstscript.cs:
using UnityEngine;
public class MyFirstScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
这是mytestscript.cs:
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Tests
{
public class MyTestScript
{
// A Test behaves as an ordinary method
[Test]
public void MyTestScriptSimplePasses()
{
var x = new MyFirstScript(); // problem occurs HERE!
// Use the Assert class to test conditions
}
// 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;
}
}
}
我希望我可以在测试代码(mytestscript(中使用类myfirstscript。我该怎么办?
查看本手册页的Monobehaviourtest部分。
请注意,在单一衍生的对象上称为"新"几乎绝不是一个好主意。您几乎总是希望通过在GameObject上调用AddComponent((来实例化。