PopeDev
PopeDev

Reputation: 33

.deps.json not found executing vs test after vs build in dev ops

I can´t past the task "execute test" because this error, in all my tests projects.

##[error]Unable to find D:\a\13\s$\testcore\obj\Debug\netcoreapp3.1\Project.TestCore.deps.json. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk".

Nuget is installed in the last version and the file deps.json always exist when build in my local machine.

Project is dot net core 3.1

Any ideas? Thank you very much in advance

Upvotes: 1

Views: 2119

Answers (2)

diegosasw
diegosasw

Reputation: 15654

I was having the same issue with .NET 6 and minimal API

This article helped https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory

The summary is that:

  • Your Microsoft.AspNetCore.Mvc.Testing must be compatible with your xUnit project. So if you're using .NET 6, make sure you use some version like 6.x.x
  • Your xUnit project MUST have a reference to the WebApi project
  • Be careful with the Startup or Program you're using to build the TestServer. If you're using a minimal Web API you have to expose the Program.cs as per link above.

In my case I added this to the Web Api project

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <InternalsVisibleToSuffix Include=".FunctionalTests" /> <!-- THIS LINE to make private classes like Program.cs available to any xUnit project with suffix .FunctionalTests-->
    <ProjectReference Include="..\ExchangeRateCalculation\ExchangeRateCalculation.csproj" />
  </ItemGroup>
</Project>

Then my minimal API looks like this

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Map("/", () => "hello world");
app.Run();

// NOTICE this declaration! This is what makes Program.cs available
partial class Program { } // See https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0

And my TestServer

public class SampleTests
{
    private readonly HttpClient _httpClient;

    public CalculateCryptoInEurTests()
    {
        var application =
            new WebApplicationFactory<Program>()
                .WithWebHostBuilder(builder =>
                {
                    builder.ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        var env = hostingContext.HostingEnvironment;

                        config
                            .AddJsonFile("appsettings.json", true, false)
                            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, false)
                            .AddEnvironmentVariables();

                        if (env.IsDevelopment())
                        {
                            var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                            config.AddUserSecrets(appAssembly, true);
                        }
                    });
                });

        var client = application.CreateClient();

        _httpClient = client;
    }

    // FACTs using the _httpClient here
}

Upvotes: 0

Leo Liu
Leo Liu

Reputation: 76770

.deps.json not found executing vs test after vs build in dev ops

According to the error message, the .deps.json exists in the web application output folder, but it is not copied to the test project output folder.

Please try to reference the nuget package Microsoft.AspNetCore.Mvc.Testing directly from the integration test project.

From the document ASP.NET Core integration tests:

The Microsoft.AspNetCore.Mvc.Testing package handles the following tasks:

  • Copies the dependencies file (.deps) from the SUT into the test project's bin directory.
  • Sets the content root to the SUT's project root so that static files and pages/views are found when the tests are executed.
  • Provides the WebApplicationFactory class to streamline bootstrapping the SUT with TestServer.

Note: deps.json file may causes this build error if project name has multiple words separated by whitespace, please try to rename the project to just one word.

Upvotes: 1

Related Questions