How do I test an async method with NUnit (or possibly with another framework)?

后端 未结 5 1118
余生分开走
余生分开走 2020-12-02 19:43

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

5条回答
  •  没有蜡笔的小新
    2020-12-02 20:32

    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.net as both support asynchronous tests.

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

提交回复
热议问题