【问题标题】:Adding an exe project as a dependency of a test project添加 exe 项目作为测试项目的依赖项
【发布时间】:2019-10-29 19:57:05
【问题描述】:

我有集成测试,它们运行在同一解决方案中生成的可执行文件,Process.Start。测试项目和exe都是net core 3.0应用。

我想在生成可执行文件的项目上向测试项目添加一个依赖项(但没有链接到它),我尝试这样:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\TestExe\TestExe.csproj">
      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
    </ProjectReference>
  </ItemGroup>

</Project>

但是当我在测试中运行可执行文件时,它会失败并出现以下错误:

A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'

当我检查我的测试项目的输出目录时,我注意到它缺少 exe 依赖项的输出目录中的 TestExe.runtimeconfig.json 文件。将此文件手动复制到测试项目输出目录即可解决问题。

表达这种依赖关系的正确方法是什么,以便所有必要的文件最终都放在测试项目的输出目录中?

【问题讨论】:

  • 你在用 Visual Studio 吗?
  • 是的,不过我可能会在 CI 中使用 dotnet 命令行工具。
  • 是否缺少参考(黄色三角形)?
  • 不。正在复制 exe 本身,而不是 runtimeconfig。
  • 似乎有一个相关问题已提交给 dotnet 团队 here

标签: c# .net-core msbuild csproj


【解决方案1】:

我通过搜索 dotnet GitHub 中的问题发现了这一点,并找到了提供解决方法的 this issue

将以下内容添加到生成您要引用的可执行文件的 .csproj 中:

  <Target Name="AddRuntimeDependenciesToContent" Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp'" BeforeTargets="GetCopyToOutputDirectoryItems" DependsOnTargets="GenerateBuildDependencyFile;GenerateBuildRuntimeConfigurationFiles">
    <ItemGroup>
      <ContentWithTargetPath Include="$(ProjectDepsFilePath)" CopyToOutputDirectory="PreserveNewest" TargetPath="$(ProjectDepsFileName)" />
      <ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)" CopyToOutputDirectory="PreserveNewest" TargetPath="$(ProjectRuntimeConfigFileName)" />
    </ItemGroup>
  </Target>

然后确保您引用了没有将 ReferenceOutputAssembly 属性设置为 false 的项目:

<ProjectReference Include="..\TestExe\TestExe.csproj"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多