为什么当异步/伺机关键字不使用异步单元测试失败? [英] Why do async unit tests fail when the async/await keywords aren't used?

查看:132
本文介绍了为什么当异步/伺机关键字不使用异步单元测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个讨论,应该有以下两种方法没有什么区别:

According to this discussion, there should be no difference between the following two methods:

public async Task Foo()
{
    await DoSomethingAsync();
}

public Task Foo()
{
    return DoSomethingAsync();
}

事实上,它似乎是非常简单的方法,调用的没有的异步/等待关键字是preferred,因为它们去掉一些开销。

Actually, it would seem that for very simple methods, the invocation without the async/await keywords would be preferred, as they remove some overhead.

然而,这显然不总是在单元测试工作。

However, this apparently doesn't always work in unit tests.

MSTest的

[TestClass]
public class AsyncTest
{
    [TestMethod]
    public async Task Test1()
    {
        await Task.Delay(0);
    }

    [TestMethod]
    public Task Test2()
    {
        return Task.Delay(0);
    }
}

NUnit的

[TestFixture]
public class AsyncTest
{
    [Test]
    public async Task Test1()
    {
        await Task.Delay(0);
    }

    [Test]
    public Task Test2()
    {
        return Task.Delay(0);
    }
}

的xUnit

public class AsyncTest
{
    [Fact]
    public async Task Test1()
    {
        await Task.Delay(0);
    }

    [Fact]
    public Task Test2()
    {
        return Task.Delay(0);
    }
}


  • 在所有情况下,测试1 通行证。

  • 在MSTest的,的Test2 显示在测试运行器,但它并没有运行。

  • 在NUnit的,的Test2 被忽略,与消息:

    • In all cases, Test1 passes.
    • In MSTest, Test2 shows up in the test runner, but it doesn't run.
    • In NUnit, Test2 is ignored, with the message:

      试验方法具有非空返回类型,但没有结果有望

      Test method has non-void return type, but no result is expected

      在的xUnit,的Test2 通行证。

    • In XUnit, Test2 passes.

      由于任务依然在所有情况下awaitable,它是什么有关影响NUnit的和MSTest的测试运行的异步关键字?也许有些反思的问题?

      Since the tasks are still awaitable in all cases, what is it about the async keyword that affects the NUnit and MSTest test runners? Perhaps some reflection issue?

      推荐答案

      这听起来像那些测试运行,可以使用反射来检查方法是否返回工作真正的的异步方法。这并不意味着该方法将表现不同的如果它们运行的 - 但他们只是没有运行。

      It sounds like those test runners may be using reflection to check whether the method returning Task really is an async method. That doesn't mean the method would behave differently if they were run - but they're just not being run.

      这就像说:

      public string Name { get; set; }
      

      相当于:

      private string name;
      public Name { get { return name; } set { name = value; } }
      

      他们的逻辑行为方面是相同的,但如果你与反思足够努力,你可以分辨出来。在这个特殊的情况下,有其它更细微的差别,但相同的一般原理也适用

      They're logically the same in terms of behaviour, but if you try hard enough with reflection, you can tell the difference. In this particular case there are other more subtle differences, but the same general principle applies.

      它看起来像当前的NUnit code(在写这篇文章的时间)的检测是<一个href=\"https://github.com/nunit/nunit/blob/51079b29113b32447d89c47c5e914f98c5875540/src/NUnitFramework/framework/Internal/AsyncInvocationRegion.cs#L81\"相对=nofollow> AsyncInvocationRegion.cs

      It looks like in the current NUnit code (at the time of this writing) the detection is in AsyncInvocationRegion.cs.

      不可否认它至少的不寻常的的编写单元测试返回工作但不使用异步方法 - 但远远不行

      Admittedly it's at least unusual to write a unit test returning a Task but without using an async method - but far from impossible.

      这篇关于为什么当异步/伺机关键字不使用异步单元测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆