Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an exe project as a dependency of a test project

I have integration tests that run an executable produced in the same solution with a Process.Start. Both the test project and the exe are net core 3.0 apps.

I'd like to add a dependency to the test project on the project that produces the executable (but without linking to it), which I tried like this:

<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>

But when I run the executable in my tests it fails with this error:

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

When I check the output directory of my test project I notice it's missing the TestExe.runtimeconfig.json file from the output directory of the exe dependency. Copying this file to the test project output directory manually fixes the problem.

What is the right way to express this dependency relationship so that all the necessary files end up in the test project output directory?

like image 728
gitbox Avatar asked Oct 29 '19 19:10

gitbox


1 Answers

I figured this one out by searching through issues in the dotnet GitHub and found this issue that provided a workaround.

Add the following to the .csproj producing the executable you want to reference:

  <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>

Then ensure you've referenced the project without the ReferenceOutputAssembly property set to false:

<ProjectReference Include="..\TestExe\TestExe.csproj"/>
like image 129
gitbox Avatar answered Nov 14 '22 22:11

gitbox