【发布时间】:2019-05-19 14:47:03
【问题描述】:
我在 Unity3d 项目中有两个文件。一个是在编辑模式下运行的测试脚本。另一个是带有静态函数的单个类,我想从测试脚本中调用它。
这是我的测试脚本:
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
public class NewTestScript
{
[Test]
public void TestAnotherStaticFunction()
{
int a = NewBehaviourScript.FunctionUnderTest(1);
int b = 1;
// Use the Assert class to test conditions.
Assert.IsTrue(a == b);
}
}
这是我正在测试的函数:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
/// <summary>
/// the stupidest function in the world,
/// used to verify tests.
/// </summary>
public static int FunctionUnderTest(int a)
{
return a;
}
}
这给了我来自 Unity 编译器的错误(我不是在 Unity 之外构建的):
Assets/TestS/NewTestScript.cs(12,17):错误 CS0103:当前上下文中不存在名称 `NewBehaviourScript'
这些在编辑模式下运行。
我已经尝试在被测函数和调用代码中添加和删除 SuperTestNameSpace 命名空间。
我尝试从由 unity 自动生成的 .asmdef 文件添加/删除文件,尽管这通常会导致其他编译错误。
我以前的单元测试经验主要是在 Visual Studio 或 VSCode 中,我正在尝试让我的 unity3d 测试经验与我之前的测试环境经验相匹配。
在编辑模式测试中是否存在根本上受限的功能,还是我遗漏了一些愚蠢的东西?
进一步阐述所涉及的程序集。看起来这里有两个程序集:Assembly-CSharp.dll 包含我的测试代码,TestS.dll 包含我的测试代码。我相信我的问题归结为:如何将 TestS.dll 程序集的引用添加到 Assembly-CSharp.dll。我知道如何在 Visual Studio 中执行此操作(通过 VS 中的上下文菜单或直接编辑 csproj 文件),但是我不知道如何在 Unity3d 中执行此操作。我对 csproj 文件所做的任何编辑都经常被统一覆盖,虽然检查器中有一个“参考”部分(见图),但我无法添加 Assembly-CSharp.dll 作为参考.
【问题讨论】:
标签: unit-testing unity3d nunit