如何使NUnit在第一次失败时停止执行测试 [英] How to make NUnit stop executing tests on first failure

查看:74
本文介绍了如何使NUnit在第一次失败时停止执行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用NUnit来执行集成测试.这些测试非常耗时.通常,检测故障的唯一方法是超时.

We use NUnit to execute integration tests. These tests are very time consuming. Often the only way to detect a failure is on a timeout.

我希望一旦检测到单个故障,测试就会停止执行.

I would like the tests to stop executing as soon as a single failure is detected.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

这可能不是理想的解决方案,但是它满足了您的要求,即,如果测试失败,则忽略其余测试.

This is probably not the ideal solution, but it does what you require i.e. Ignore remaining tests if a test has failed.

[TestFixture]
    public class MyTests
    {
        [Test]
        public void Test1()
        {
            Ascertain(() => Assert.AreEqual(0, 1));
        }

        [Test]
        public void Test2()
        {
            Ascertain(() => Assert.AreEqual(1, 1));
        }

        private static void Ascertain( Action condition )
        {
            try
            {
                condition.Invoke();
            }

            catch (AssertionException ex)
            {
                Thread.CurrentThread.Abort();
            }
        }
    }

由于TestFixtureAttribute是可继承的,因此您可以潜在地创建一个在其上装饰了此属性的基类,并在其中具有确定保护的方法,并从中派生所有TestFixture类.

Since TestFixtureAttribute is inheritable, so you could potentially create a base class with this attribute decorated on it and have the Ascertain protected Method in it and derive all TestFixture classes from it.

唯一的缺点是,您必须重构所有现有的断言.

The only downside being, you'll have to refactor all your existing Assertions.

这篇关于如何使NUnit在第一次失败时停止执行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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