18

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?

willem
  • 25,977
  • 22
  • 75
  • 115
  • I believe this is tests-runner specific questiion so how do you run tests? NUnit-console, msbuild NUnit task, an other runner? – sll Oct 05 '11 at 16:31

3 Answers3

29

Using nunit-console, you can achieve this by using the /stoponerror command line parameter.

See here for the command line reference.

For nunit-console v3, it changes to --stoponerror (see here for the command line reference).

Andrew McClement
  • 1,171
  • 5
  • 14
Lukazoid
  • 19,016
  • 3
  • 62
  • 85
  • 5
    I believe this is a better approach. You let NUnit handle this instead of intervening in the process and also polluting the testing code with unnecessary stuff! – mosu Jun 23 '14 at 16:20
23

I'm using NUnit 3 and the following code works for me.

public class SomeTests {
    private bool stop;

    [SetUp]
    public void SetUp()
    {
        if (stop)
        {
            Assert.Inconclusive("Previous test failed");
        }
    }

    [TearDown]
    public void TearDown()
    {
        if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
        {
            stop = true;
        }
    }
}

Alternatively you could make this an abstract class and derive from it.

-1

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

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.

Raghu
  • 2,678
  • 2
  • 31
  • 38
  • It's a step in the right direction. It will only work for Asserts though, and not exceptions. But it's an interesting idea... I might be able to do something similar for exceptions. – willem Oct 05 '11 at 08:29
  • @willem Can you please post your workaround for exceptions if you succeed first. I'll keep trying as well. – Raghu Oct 05 '11 at 09:57
  • Could you not wrap the whole test method body in Ascertain and just capture all exceptions? – Andy Morris Oct 05 '11 at 10:09