Martin Ba
Martin Ba

Reputation: 38961

Can NUnit Console Runner report failed tests on the fly?

nunit-console seems to work like this (params: /nologo /wait /labels ...)

It outputs only the label for each test when a test is started, like so:

***** Our.Tests.Bla
... here come some Console.WriteLine
// Note that in case Our.Tests.Bla fails, nothing is reported here immediately
***** Our.Tests.Blub
... here come some Console.WriteLine
***** Our.Tests.Foo
... here come some Console.WriteLine

If any test fails, it reports the test failures only at the end of the complete run.

Normally this is fine, but we're also running some interdependent integration tests via NUnit, and sometimes one test hangs because a previous test failed.

The problem is, when one test hangs, you do not see which / if any previous test failed, making it so much harder to quickly track down the problem. (Especially when the tests are hanging on the test server machine, or maybe you only have a log of an aborted run.)

I would really prefer for NUnit to report failed tests, including the detailed error, on the fly / immediately before starting the next test. Is this possible?

Upvotes: 2

Views: 1481

Answers (4)

Justin
Justin

Reputation: 3443

you can use /labels. this will at least help you locate the failed test.

Upvotes: 0

Wosi
Wosi

Reputation: 45391

This answer is based on Andrew's answer

Some classes changed in NUnit 3 so the TearDown method needs to be changed too. The result looks like this:

[TearDown]
public void TearDown()
{
    var status = TestContext.CurrentContext.Result.FailCount;
    if (status > 0)
        Console.WriteLine("Test Failed: {0}\r\nMessage:\r\n{1}", 
                           TestContext.CurrentContext.Test.FullName, 
                           TestContext.CurrentContext.Result.Message);          
} 

Upvotes: 1

Andrew
Andrew

Reputation: 143

One way you could do this to have a Teardown in your tests file, which runs on each test completing, which can then output the name of the test if it failed.

    [TearDown]
    public void TearDown()
    {
        var status = TestContext.CurrentContext.Result.Status;
        if(status != TestStatus.Passed)
          Console.WriteLine("Test Failed: {0}", TestContext.CurrentContext.Test.FullName);   
    }

Upvotes: 4

48klocs
48klocs

Reputation: 6113

Another question on here asked about having the console runner halt on the first error that it encountered.

The /stoponerror option is not what you asked for, but it's likely going to be about as good as you'll get.

Upvotes: 1

Related Questions