将ASP.NET Core与.NET Framework进行集成测试

编程入门 行业动态 更新时间:2024-10-26 16:23:51
本文介绍了将ASP.NET Core与.NET Framework进行集成测试-找不到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.Tests \ bin \ Debug \ TestRepro.deps.json".该文件是功能测试正常运行所必需的.源项目bin文件夹上应该有该文件的副本.如果不是这种情况,请确保在项目文件上将属性PreserveCompilationContext设置为true.例如"true".为了使功能测试正常工作,它们需要从构建输出文件夹中运行,或者必须将应用程序输出目录中的TestRepro.deps.json文件复制到运行测试的文件夹中.导致此错误的常见原因是在运行测试时启用了卷影复制.

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.

我已经验证了Web应用程序输出文件夹(TestRepro \ bin \ Debug \ net47)中是否存在TestRepro.deps.json,但是没有将其复制到测试项目输出文件夹(TestRepro.Tests \ bin \ Debug)中.而且我还无法找出如何禁用卷影复制.

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.

编辑:文档说:

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上有 repro .

I have a repro on GitHub if that helps.

推荐答案

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

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

  • 创建 New Project->xUnit测试项目(.Net核心)
  • 右键单击新项目->编辑.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

  • 更多推荐

    将ASP.NET Core与.NET Framework进行集成测试

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

    发布评论

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

    >www.elefans.com

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