Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 837
Henrik Olsson Avatar asked Mar 12 '19 22:03

Henrik Olsson


People also ask

What is DEPS JSON file .NET core?

deps. json file, which lists the dependencies of the application or library. A . runtimeconfig. json file, which specifies the shared runtime and its version for an application.

Which package component can be added for integration testing of the .NET core API applications?

WebApplicationFactory<TEntryPoint> is used to create a TestServer for the integration tests. TEntryPoint is the entry point class of the SUT, usually the Startup class.

Where are integration tests stored?

Integration tests are saved in a separate source folder. These tests are different from unit tests in that they use real data and/or thirds party APIs. As a result they tend to be slower as they make network requests, perform database queries, and use large data sets instead of mocking data.


2 Answers

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.

like image 141
TylerOhlsen Avatar answered Sep 23 '22 08:09

TylerOhlsen


Follow steps below to create Integration Test for Asp.Net Core with targeting net 47.

  1. Create New Project-> xUnit Test Project(.Net Core)
  2. Right click new project->Edit .csproj->Change TargetFramework to net47
  3. Add Project Reference to TestRepro
  4. Install-Package Microsoft.AspNetCore.Mvc.Testing
  5. Add Test file like below

    public class BasicTests
    : IClassFixture<WebApplicationFactory<Startup>>
    {
        private readonly WebApplicationFactory<Startup> _factory;
    
        public BasicTests(WebApplicationFactory<Startup> factory)
        {
            _factory = factory;
        }
    
        [Fact]
        public async Task TestMethod1()
        {
            var client = _factory.CreateClient();
            var response = await client.GetAsync("/api/values");
        }
    
    }
    
  6. Run Test Project

like image 20
Edward Avatar answered Sep 24 '22 08:09

Edward