【问题标题】:Xunit test not copying appsettings file to output directoryXunit 测试未将 appsettings 文件复制到输出目录
【发布时间】:2020-11-11 18:24:40
【问题描述】:

我有一个针对 .NET Core 3.1 和 .NET Framework 4.6.1 的 Xunit 测试项目。我的测试加载了一个 appsettings.json 文件as detailed by Rick Strahl。测试运行时,即使我已将文件属性设置为 Content 类型并始终复制到输出目录,但测试运行时该文件实际上并不存在。

现在我成功地在另一个单元测试项目中使用了这种技术,但不同之处在于它只针对 .NET Core 3.1。在工作测试项目中,Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)C:\git\myproject\src\myproject.tests\bin\Debug\netcoreapp3.1(即构建输出目录)。但是,在失败的测试中,位置是 C:\Users\hcheng\AppData\Local\Temp\ffe98278-56d3-41f6-8864-e50c74cb2c08\ffe98278-56d3-41f6-8864-e50c74cb2c08\assembly\dl3\8ba8de87\6b2a96ca_54b8d601,当我打开 Windows 资源管理器到该路径时,该文件夹中的唯一内容是单元测试项目的 DLL 和 PDB,以及 __AssemblyInfo__.ini(其他都没有依赖项也在那里)。

我对 .NET Core 完全陌生,所以我不能真正说出我做错了什么,但我怀疑多目标是相关的。由于该临时位置缺少其他依赖项,我还担心如果我克服了加载配置文件的障碍,之后我仍然会收到 TypeLoadExceptions。

提前感谢您的帮助。

【问题讨论】:

    标签: .net-core visual-studio-2019 xunit


    【解决方案1】:

    我最终在How to refer to test files from Xunit tests in Visual Studio?找到了答案。

    我不得不使用new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath,而不是Assembly.GetExecutingAssembly().Location

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 2013-12-16
      • 2017-11-06
      • 2017-03-10
      • 1970-01-01
      相关资源
      最近更新 更多