【发布时间】:2018-12-25 18:01:55
【问题描述】:
使用 Unity 2018.1.1f1 和 Visual Studio Community 2017
我一直在使用 unity 内置的 Test Runner 创建单元测试。在这个特定的单元测试中,我想将文件的内容读入字符串。无需构建文件的静态路径(它将位于单元测试类本身旁边),我如何引用它?
文件夹结构如下:
Directory:
Assets/Plugins/MyTools/Common/Properties/_tests/
Files:
MyTools.Common.Properties.Tests.asmdef
PropertiesTests.cs
test.txt
test-prop-file.txt
测试和一些输出细节:
namespace MyTools.Common.Properties.Tests {
public class PropertiesTests {
[Test]
public void ParsePropsFile() {
Debug.Log(Assembly.GetExecutingAssembly().GetName());
// MyTools.Common.Properties.Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Debug.Log(GetType().Assembly.GetManifestResourceNames().Length);
// 0
Debug.Log(Assembly.GetExecutingAssembly().GetManifestResourceNames().Length);
// 0
}
}
}
在 Visual Studio 中,我尝试设置 test-prop-file.txt 和 test.txt 的属性(构建操作 = 嵌入式资源,复制到输出目录 = 始终复制),但它们似乎没有这样做任何事物。此外,当我关闭 VS 并重新打开它时,这些设置会恢复到以前的状态(无和不复制)。我也尝试过其他组合,例如 Build Action = Resource 或 Content,但仍然没有。
那么我如何才能在单元测试中加载这些文件而不直接使用静态路径(如 D:/Code/Unity/MyProject/Assets/Plugins/MyTools/Common/Properties/_tests/test.txt)引用它们?
【问题讨论】:
-
不是最好的解决方案,但您可以使用
TestContext.CurrentContext.TestDirectory + "/../../Assets/Plugins/MyTools/Common/Properties/_tests/"。您可能需要或多或少地添加/../
标签: c# visual-studio unit-testing unity3d nunit