Question

I run NUnit console on my test assembly but sometimes my NUnit crashes (I'm currently figuring out why). When nunit crashes no xml report is created and I effectively am left without any test report. The test run is very long and it's a bad situation if I lose the entire test run report. Is there a way to tell NUNit to write the xml log file as it goes (after each test). Even if that means the xml file won't be closed properly. I know it is possible to use NUnit TestListener. But is there a switch or something that does not include writing extra code myself.

Was it helpful?

Solution

You could invoke nunit-console with the /labels command-line switch. This will cause it to write out the (fully-qualified) name of each test to the console as it runs, though the output does not include the test result. However, it would show you which tests had run successfully.

The only other option is the one you have discounted, that of writing your own EventListener implementation. The documentation about that interface is a little light, but the NUnit documentation about writing an NUnit add-in assembly should be enough to get you started if you change your mind about that.

OTHER TIPS

First of all in a near future it looks like such a logging will be provided by NUnit out of the box - this feature was already requested - Write partial results XML as tests happen instead of at the end.

Until its done we need to write some custom code and in case u use NUnit < 3.x its pretty simple:
1)You need to create an event listener which implements EventListener interface
2)This interface provides notifications for different states of test execution eg. RunStarted, RunFinished or TestFinished. All you need to is an implementation of logging relevant for your case. Snippet from my code:

 public void RunStarted(string name, int testCount)
 {
     try
     {
         _instanceId = Environment.GetEnvironmentVariable("InstanceId");
         _buildId = Environment.GetEnvironmentVariable("BuildId");
         _browser = Environment.GetEnvironmentVariable("BrowserToTest");
         _template = Environment.GetEnvironmentVariable("TemplateToTest");
     }
     catch { }
 }

 public void TestFinished(TestResult result)
 {
     if (result.ResultState == ResultState.Ignored)
     {
         return;
     }
     var r = new TestingWorkerData
     {
         BuildId = _buildId,
         InstanceId = _instanceId,
         TestName = result.FullName,
         Success = result.IsSuccess,
         TimeTaken = result.Time.ToString(CultureInfo.InvariantCulture),
         Message = result.Message,
         StackTrace = result.StackTrace,
         Browser = _browser,
         Template = _template
     };         
     File.AppendAllLines(@"z:\\results.txt", new[] {JsonConvert.SerializeObject(r)});
 }

 public class TestingWorkerData
 {
     public string TestName { get; set; }
     public bool Success { get; set; }
     public string TimeTaken { get; set; }
     public string Message { get; set; }
     public string StackTrace { get; set; }
     public string InstanceId { get; set; }
     public string Browser { get; set; }
     public string Template { get; set; }
     public string BuildId { get; set; }
 }

3)The last thing is creation of NUnit addin:

[NUnitAddin]
public class ProgressReporterNugetAddin : IAddin
{
    public bool Install(IExtensionHost host)
    {
        var listeners = host.GetExtensionPoint("EventListeners");
        listeners.Install(new ProgressReporterEventListener());
        return true;
    }
}

Note:There is a pretty good article http://jimmykeen.net/2015/02/28/logging-test-results-with-nunit/ which covers similar approach and more.
Unfortunately it works for NUnit < 3.x ONLY since NUnit3 was heavily rewritten - eg. no EventListener interface anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top