.deps.json 在 devops 中 vs build 之后未发现正在执行 vs 测试

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

我无法通过“执行测试”任务,因为这个错误,在我所有的测试项目中。

##[错误]无法找到 D:\a\s$\testcore\obj\Debug\netcoreapp3.1\Project.TestCore.deps.json。确保测试项目具有包“Microsoft.NET.Test.Sdk”的 nuget 引用。

Nuget 已安装在最新版本中,并且文件 deps.json 在我的本地计算机中构建时始终存在。

项目是.net core 3.1

有什么想法吗?非常感谢您

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

根据错误信息,.deps.json存在于web应用程序输出文件夹中,但没有复制到测试项目输出文件夹中。

请尝试直接从集成测试项目中引用 nuget 包 Microsoft.AspNetCore.Mvc.Testing

来自文档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.

注意: deps.json 文件如果项目名称有多个由空格分隔的单词,可能会导致此构建错误,请尝试将项目重命名为一个词。

我在 .NET 6 和最小 API

中遇到了同样的问题

这篇文章有帮助https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory

总结如下:

  • 您的 Microsoft.AspNetCore.Mvc.Testing 必须与您的 xUnit 项目兼容。因此,如果您使用的是 .NET 6,请确保使用 6.x.x
  • 等版本
  • 您的 xUnit 项目必须引用 WebApi 项目
  • 小心你用来构建测试服务器的 StartupProgram。如果您使用的是最小 Web API,则必须按照上面的 link 公开 Program.cs

在我的例子中,我将其添加到 Web Api 项目

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

然后我的最小 API 看起来像这样

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://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0

还有我的测试服务器

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
}