Wermuth
Wermuth

Reputation: 1

NUnit Extensibility - Get failure messages

I have been searching for some time for ways to get the error message in NUnit. So far, I found some classes that use IAddin and EventListener to get the message, but it didn't work. I based this code on this links:

About extensions

Source code

Extensability

I added the code to the project, but nothing is being recorded, or being done when the tests failed. I read that I have to add the dll to some "NUnit\addins\" folder, but I couldn't find any addin folder with this "addins".

I don't know what I'm missing, can someone help me?

Below is the code that I used:

using System;
using System.IO;
using NUnit.Core;
using NUnit.Core.Extensibility;

namespace Test
{
    [NUnitAddinAttribute(Type = ExtensionType.Core,
                         Name = "Database Addin",
                         Description = "Writes test results to the database.")]
    public class MyNunitExtension : IAddin, EventListener
    {
        public bool Install(IExtensionHost host)
        {
            IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
            if (listeners == null)
                return false;

        listeners.Install(this);
        return true;
    }

    public void RunStarted(string name, int testCount) { }
    public void RunFinished(TestResult result) { }
    public void RunFinished(Exception exception) { }
    public void TestStarted(TestName testName) { }

    public void TestFinished(TestResult result)
    {
        using (var arq = File.Open(@"C:\Temp\Log.txt", FileMode.Append))
        using (var writer = new StreamWriter(arq))
        {
            var message = string.Format("[{0:s}] [{1}] {2}", DateTime.Now,
                result.ResultState, result.Name);
            writer.WriteLine(message);
            var isFailure =
                result.ResultState == ResultState.Error ||
                result.ResultState == ResultState.Failure;
            if (isFailure)
            {
                writer.WriteLine(result.Message);
            }
        }
    }

    public void SuiteStarted(TestName testName) { }
    public void SuiteFinished(TestResult result) { }
    public void UnhandledException(Exception exception) { }
    public void TestOutput(TestOutput testOutput) { }
}
}

Upvotes: 0

Views: 386

Answers (1)

Jo Biesta
Jo Biesta

Reputation: 51

Hopefully this will help you out a little.

I'm currently looking at addins for Nunit too and would suggest you check the following: The solution you've got your addin file above in needs to be setup with a target framework of .net 3.5 ( see discussion here: https://groups.google.com/d/msg/nunit-discuss/je0VXIsVQNQ/UpvDmYEkVhAJ ). When you build this solution take the .dll it creates from the bin folder and place it into the addins directory for nunit which can be found in the installation directory, in my case ( C:\Program Files (x86)\NUnit 2.6.4\bin\addins ).

Then when you open up Nunit go to Tools > Addins and you should see your addin there.

Upvotes: 1

Related Questions