'Integration testing ASP.NET Core with .NET Framework - can't find deps.json
I have a ASP.NET Core Web API project targeting .NET Framework 4.7 that I'm trying to write integration tests for. I created a unit test project using Visual Studio Add new project and then the Unit Test Project (.NET Framework) template. I added the Microsoft.AspNetCore.Mvc.Testing NuGet package to the test project, and I have the following test:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestRepro.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public async Task TestMethod1()
{
var factory = new WebApplicationFactory<Startup>();
var client = factory.CreateClient();
var response = await client.GetAsync("/api/values");
}
}
}
But this throws the following exception:
Test method TestRepro.Tests.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Can't find'[path removed]\TestRepro.Tests\bin\Debug\TestRepro.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 'true'. For functional tests to work they need to either run from the build output folder or the TestRepro.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.
I have verified that TestRepro.deps.json exists in the web application output folder (TestRepro\bin\Debug\net47), but it is not copied to the test project output folder (TestRepro.Tests\bin\Debug). And I have not been able to find out how to disable shadow copying.
Edit: The documentation says:
The Microsoft.AspNetCore.Mvc.Testing package handles the following tasks: Copies the dependencies file (*.deps) from the SUT into the test project's bin folder.
But that doesn't seem to work. I can copy the file manually, but that doesn't work in a automated build scenario. One way would be to have a build step doing it in TeamCity, but it feels crude. Any ideas?
I have a repro on GitHub if that helps.
Solution 1:[1]
I just ran into this same issue and found the root cause to be quite obscure. From the documentation, the .deps files should be copied to the integration test project's bin directory. This wasn't happening for me because was not explicitly referencing the Microsoft.AspNetCore.Mvc.Testing package from my integration test project. I had created a shared library with some utility functions that referenced that nuget package, so my integration test project indirectly referenced it.
There's some custom build tasks in the Microsoft.AspNetCore.Mvc.Testing package that copy the referenced service deps.json files for you, so you must reference it directly from the integration test project in order to get those build tasks to run.
Solution 2:[2]
I got the same error when integration testing in .NET Core 6.
For integration, we should get "Program" class from the main project but it automatically references Microsoft.AspNetCore.Mvc.Testing.Program class
public class VersioningTests : IClassFixture<WebApplicationFactory<Program>>
this code must reference our main Program code. So we must put reference of Program class on the last line in Program.cs file:
public partial class Program { }
Solution 3:[3]
For me, I already had a reference to Microsoft.AspNetCore.Mvc.Test in my test project, but removing the following fixed the issue:
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Solution 4:[4]
Okay, after almost two hours of trying to solve this... I got it.
My issue was I was using the new .NET6 Minimal APIs Program.cs format which does not have Program class (nor a namespace) defined and I was literally referencing the wrong class in the WebApplicationFactory<Program> definition... it came from Microsoft.VisualStudio.TestPlatfrom.TestHost (seriously WTF), so what you need to do is:
- What you need to do is use your own actual Program.cs, so you add a namespace to your Minimal APIs Program.cs file and create an actual Program class with a Main method.
- Now you can reference the proper Program class as the generic type for the WebApplicationFactory
- Make sure you make internals visible in the web app project so you can expose the Program class (if you marked it internal).
This is basically an additon to Ahmets answer here: https://stackoverflow.com/a/70490057/1534753
Solution 5:[5]
Accepted answer assumes that deps.json file exists in your project bin folder, so it can be copied into integration test bin folder.
In my case deps.json file was missing in application bin folder.
In this case make sure you have
<PreserveCompilationContext>true</PreserveCompilationContext>
in your csproj file.
Solution 6:[6]
I'm facing the same issue and I want to share with you a way to make it copy to publish below:
Find the .csprj test file Add the following code would help you get through.
p/s I'm using .net core 3.1, so it would be useful for .net core test prj.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<Target Name="CopyDepsJsonFiles" AfterTargets="Publish">
<ItemGroup>
<DepsJsonFiles Include="$(TargetDir)*.deps.json" />
</ItemGroup>
<Copy SourceFiles="@(DepsJsonFiles)" DestinationFolder="$(PublishDir)" />
</Target>
<!-- ... other elements-->
</Project>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TylerOhlsen |
| Solution 2 | Ahmet Arslan |
| Solution 3 | tomliversidge |
| Solution 4 | Vedran Mandić |
| Solution 5 | Arek Felinczak |
| Solution 6 |
