使用 Unity 2018.1.1f1 和 Visual Studio Community 2017
我一直在使用 Unity 的内置测试运行器创建单元测试。在这个特定的单元测试中,我想将一个文件的内容读入一个字符串中。无需构建文件的静态路径(它将位于单元测试类本身旁边),我如何引用它?
文件夹结构如下所示:
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 的属性(Build Action = Embedded Resource,以及 Copy to Output Directory = Copy Always),但他们似乎没有这样做任何事物。此外,当我关闭 VS 并重新打开它时,这些设置回到它们之前的状态(无和不复制)。我也尝试过其他组合,例如 Build Action = Resource 或 Content,但仍然一无所获。
那么我怎样才能在单元测试中加载这些文件而不用像 D:/Code/Unity/MyProject/Assets/Plugins/MyTools/Common/Properties/_tests/test.txt 这样的静态路径直接引用它们呢?
最佳答案
可以通过以下方式检索当前测试程序集的位置:
TestContext.CurrentContext.TestDirectory
(如果我没记错的话 - 中间的“CurrentContext”可能不是必需的!)
关于c# - 在 Unity3d 的 Unity Test Runner 中的单元测试期间加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391472/