NUnit在单独的线程中断言 [英] NUnit asserts in separate thread

查看:108
本文介绍了NUnit在单独的线程中断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nunit是否适用于多线程上下文. 我当然不想让nunit测试我的多线程应用程序,但想使用nunit断言

简单示例-此测试为绿色".我怎么了?

[Test]
public void Test()
{
  Action action = Async;
  action.BeginInvoke(action.EndInvoke, null).AsyncWaitHandle.WaitOne();
}

private void Async()
{
  Assert.IsTrue(false);
  Assert.DoesNotThrow( () =>
                       {
                         Console.WriteLine("Async");
                         throw new InvalidOperationException();
                       });
}

解决方案

NUnit测试运行程序可执行文件配置为忽略非测试线程上的异常.您可以通过app.config配置元素配置此行为:

legacyUnhandledExceptionPolicy

您可以将非测试线程配置为导致未处理的异常,因此测试将失败.需要注意的是,在运行另一个测试时可能会引发非测试线程异常,因此您可能无法准确了解哪个测试失败.另外,由于这将是未处理的异常,因此测试执行将在异常发生时停止,而不是继续进行,这是正常行为.

在我看来,总比不理会.

以下文章详细介绍了ReSharper测试运行程序的主题,但是原理是相同的.

ReSharper测试运行程序–隐藏的线程异常

如果您确实有多线程方面,那么从断言的角度来看,我发现最好在非测试线程上设置标志等,等待非测试线程完成,然后再声明状态测试线程.这样,可以在主线程上引发断言异常,并且测试可以按预期运行.

设计示例

    [Test]
    public void Test()
    {
        Exception ex = null;

        Action test = () =>
        {
            Console.WriteLine("Async");

            ex = new InvalidOperationException();
        };

        test.BeginInvoke(test.EndInvoke, null).AsyncWaitHandle.WaitOne();

        Assert.That(ex, Is.Null, "Exception did not happen.");
    }

Is nunit applicable in multithreading context. I do not want nunit to test my multithreading application of course, but want to use nunit assertions

Simple example - this test is "green". What do I wrong?

[Test]
public void Test()
{
  Action action = Async;
  action.BeginInvoke(action.EndInvoke, null).AsyncWaitHandle.WaitOne();
}

private void Async()
{
  Assert.IsTrue(false);
  Assert.DoesNotThrow( () =>
                       {
                         Console.WriteLine("Async");
                         throw new InvalidOperationException();
                       });
}

解决方案

The NUnit test runner executable is configured to ignore exceptions on non-test threads. You can configure this behaviour via app.config configuration element:

legacyUnhandledExceptionPolicy

You can configure non-test threads to cause unhandled exceptions, and so tests will then fail. The caveat is that non-test thread exceptions may be raised whilst another test is running so you may not get an accurate picture of which test failed. Also, as this will be an unhandled exception, test execution will stop at the point the exception happens, rather then continue which is the normal behaviour.

Better than ignoring though in my opinion.

The following article goes into some detail on the topic for the ReSharper test runner, but the principles are the same.

ReSharper test runner – hidden thread exceptions

If you do have multi-threading aspects, from an assertion point of view, I find it better to set flags etc on the non-test thread, wait for the non-test thread to finish, and then assert the state on the test thread. In this way assertion exceptions are raised on the main thread and tests run as expected.

Contrived Example

    [Test]
    public void Test()
    {
        Exception ex = null;

        Action test = () =>
        {
            Console.WriteLine("Async");

            ex = new InvalidOperationException();
        };

        test.BeginInvoke(test.EndInvoke, null).AsyncWaitHandle.WaitOne();

        Assert.That(ex, Is.Null, "Exception did not happen.");
    }

这篇关于NUnit在单独的线程中断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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