使用 .NET Framework 集成测试 ASP.NET Core

编程入门 行业动态 更新时间:2024-10-26 18:22:21
本文介绍了使用 .NET Framework 集成测试 ASP.NET Core - 找不到 deps.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个面向 .NET Framework 4.7 的 ASP.NET Core Web API 项目,我正在尝试为其编写集成测试.我使用 Visual Studio 添加新项目和单元测试项目 (.NET Framework) 模板创建了一个单元测试项目.我将 Microsoft.AspNetCore.Mvc.Testing NuGet 包添加到测试项目中,我有以下测试:

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"); } } }

但这会引发以下异常:

测试方法TestRepro.Tests.UnitTest1.TestMethod1抛出异常:System.InvalidOperationException:找不到'[路径已删除]TestRepro.TestsinDebugTestRepro.deps.json'.此文件是功能测试正常运行所必需的.您的源项目 bin 文件夹中应该有该文件的副本.如果不是这种情况,请确保在项目文件中将属性 PreserveCompilationContext 设置为 true.例如真实".要使功能测试正常工作,它们需要从构建输出文件夹运行,或者必须将应用程序输出目录中的 TestRepro.deps.json 文件复制到运行测试的文件夹中.此错误的常见原因是在测试运行时启用了卷影复制.

Test method TestRepro.Tests.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Can't find'[path removed]TestRepro.TestsinDebugTestRepro.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.

我已验证 Web 应用程序输出文件夹 (TestReproinDebug et47) 中存在 TestRepro.deps.json,但它没有复制到测试项目输出文件夹 (TestRepro.TestsinDebug).而且我还没有找到如何禁用卷影复制.

I have verified that TestRepro.deps.json exists in the web application output folder (TestReproinDebug et47), but it is not copied to the test project output folder (TestRepro.TestsinDebug). And I have not been able to find out how to disable shadow copying.

文档说:

Microsoft.AspNetCore.Mvc.Testing 包处理以下任务:将依赖项文件 (*.deps) 从 SUT 复制到测试项目的 bin 文件夹中.

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.

但这似乎不起作用.我可以手动复制文件,但这在自动构建场景中不起作用.一种方法是在 TeamCity 中进行构建步骤,但感觉很粗糙.有什么想法吗?

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?

如果有帮助的话,我有 GitHub 上的一个副本.

I have a repro on GitHub if that helps.

推荐答案

按照以下步骤为 Asp.Net Core 创建集成测试,目标为 net 47.

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

  • 创建新建项目->xUnit 测试项目(.Net Core)
  • 右键新建项目->编辑.csproj->将TargetFramework改为net47
  • 将项目引用添加到 TestRepro
  • 安装包Microsoft.AspNetCore.Mvc.Testing
  • 添加如下测试文件

  • Create New Project-> xUnit Test Project(.Net Core)
  • Right click new project->Edit .csproj->Change TargetFramework to net47
  • Add Project Reference to TestRepro
  • Install-Package Microsoft.AspNetCore.Mvc.Testing
  • 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"); } }

  • 运行测试项目

  • Run Test Project

  • 更多推荐

    使用 .NET Framework 集成测试 ASP.NET Core

    本文发布于:2023-11-13 10:16:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1584112.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:测试   Framework   NET   ASP   Core

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!