c# - 在 Unity3d 的 Unity Test Runner 中的单元测试期间加载文件

标签 c# visual-studio unit-testing unity3d nunit

使用 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/

相关文章:

c# - 如何使用 C# 将 JSON 文本转换为对象

c# - 如何在我的 ASP.NET 站点中显示另一个站点的 RSS 提要?

c# - Assert.IsTrue(false) 正在通过

.net - 如何在多进程系统上使用单个proc进行调试?

unit-testing - 测试自耕农的 composeWith

AngularFireDatabase、Jest 和单元测试 firebase 实时数据库

c# - 使用 DataLakeStore FileSystem ManagementClient 上传到 Azure Data Lake 的 30Mb 限制

C# 程序查询网络文件夹中的 Access 数据库比查询本地副本花费的时间更长

visual-studio - Visual Studio 安装过程中发生访问被拒绝

python - 模拟一个对象的返回值