如何测试一个异步方法NUnit的,最终与另一个框架?

编程入门 行业动态 更新时间:2024-10-25 00:35:56
本文介绍了如何测试一个异步方法NUnit的,最终与另一个框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ASP.NET Web API的应用程序,与ApiController,具有异步方法,返回任务<> 对象,并标有异步关键字。

I have an ASP.NET Web API application, with an ApiController that features asynchronous methods, returning Task<> objects and marked with the async keyword.

public class MyApiController : ApiController { public async Task<MyData> GetDataById(string id) { ... } }

我怎么能写NUnit的测试的ApiController的异步方法?如果我需要使用另一个测试框架,我开了这一点。我是相当新的一般的.NET单元测试,所以我有兴趣学习的最佳做法。

How can I write NUnit tests for the ApiController's asynchronous methods? If I need to use another testing framework I'm open for that too. I'm fairly new to .NET unit testing in general, so I'm interested in learning best practices.

推荐答案

在我看来,没有支持内置NUnit的2.6测试异步方法返回的任务。我可以看到,现在最好的办法是使用Visual Studio自己UnitTesting的框架或xUnit 既支持异步测试。

It seems to me there is no support built into NUnit 2.6 for testing async methods returning Tasks. The best option I can see right now is to use Visual Studio's own UnitTesting framework or xUnit as both support asynchronous tests.

与Visual Studio UnitTesting框架,我可以编写异步测试是这样的:

With the Visual Studio UnitTesting framework I can write asynchronous tests like this:

using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class TestAsyncMethods { [TestMethod] public async Task TestGetBinBuildById() { ... var rslt = await obj.GetAsync(); Assert.AreEqual(rslt, expected); } }

更多推荐

如何测试一个异步方法NUnit的,最终与另一个框架?

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

发布评论

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

>www.elefans.com

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