xUnit - deps.json not created for Integration Test in .NetCore 3.1 [Api.deps.json'. This file is required for functional tests to run properly.]

Viewed 1707

I have a visual studio solution with 2 projects, both in .Net Core 3.1

Xyz.Api, Xyz.ApiTests

ApiTests is a xUnit project for integration testing.

I have followed what has been mentioned here. Integration Tests in .NET Core

This is what ApiTests.csproj looks like

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

    <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.8" />
    <PackageReference Include="Microsoft.NETCore.App" Version="2.2.8" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>
    <ItemGroup>
    <ProjectReference Include="..\Xyz.Api\Xyz.Api.csproj" />
  </ItemGroup>
</Project>

I am able to run the tests from Visual Studio and it works fine.

But when I publish the ApiTests project, it doesn't create Xyz.Api.deps.json file which is required to run test using .net core cli.

This is the error I get with command "dotnet test Xyz.ApiTests.dll"

System.InvalidOperationException : Can't find'D:\T\Xyz.Api.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. 

I need to do this to make sure it works in Azure Pipeline.

Is there anything I am missing, or is there any other way to do this?

3 Answers

I also faced the same issue. Didn't find any special msbuild parameters, so just added this to my Tests.csproj

<Target Name="CopyDepsJsonFiles" AfterTargets="Publish">
  <ItemGroup>
    <DepsJsonFiles Include="$(TargetDir)*.deps.json"/>
  </ItemGroup>
  <Copy SourceFiles="@(DepsJsonFiles)" DestinationFolder="$(PublishDir)" />
</Target>

Try to disable shadow copy by adding the xunit.runner.json file into the ApiTests.csproj

<ItemGroup>
    <None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

File content should look like this

{
  "shadowCopy": false
}  

I was banging my head against a wall because of this for a couple of hours right now and for the reason was a missing variable. the reason was in plain sight, I was just not seeing the trees for the forest:

System.InvalidOperationException : 
    Can't find'/home/vsts/work/1/s/Code/.../bin/$(BuildConfiguration)/netcoreapp3.1/xxx.deps.json'. This file is required for functional ...

I didn't releazie that $(BuildConfiguration) was simply not defined, yet.

So I set it in the variables section, and it all was fine again:

variables:
  tag: '$(Build.BuildId)'
  dockerRegistryServiceConnection: 'xxx'
  imageBaseName: 'xxx'
  containerRegistry: 'xxx.azurecr.io'
  BuildConfiguration: 'Release'
Related