Bra*_*rad 3 nunit unit-testing unity-game-engine
我在 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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在测试的功能:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我来自 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 文件所做的任何编辑经常被 unity 覆盖,虽然检查器中有一个“参考”部分(见图),但我无法添加Assembly-CSharp.dll作为参考。
这些是 TestS.asmdef 的检查器设置。虽然可以选择添加引用,但我无法添加对 Assembly-CSharp.dll 的引用,这是我的被测代码所在的位置。
好的,我想通了。发生了两件事:
默认情况下,unity 会将您的脚本代码添加到名为Assembly-CSharp.dll的程序集,并且由于未知原因,我的编辑模式测试代码未引用此程序集。我不确定这是 Unity 中的错误还是设计使然,但显式创建和引用程序集定义已经解决了问题。
| 归档时间: |
|
| 查看次数: |
3298 次 |
| 最近记录: |