3

I want to add a custom test reporter to NUnit. I already did it with NUnit2, but I now need to use NUnit3.

To implement the reporter, I need to get various events from the framework, like start, end and failure of tests.

In NUnit2 I used NUnitHook to register my EventListener and it worked pretty good.

In NUnit3 I need to use the extension point mechanism, but when I add the extension point to the project, VisualStudio (2012 ultimate) immediately fails to discover the NUnit tests.

[TypeExtensionPoint(Description = "Test Reporter Extension")]
    public class MyTestEventListener : ITestEventListener
    {

        public void OnTestEvent(string report)
        {
            Console.WriteLine(report);
        }

    }

If I remove the ITestEventListener implementation declaration from the class, it rediscovers the tests perfectly.

[TypeExtensionPoint(Description = "Test Reporter Extension")]
    public class MyTestEventListener //: ITestEventListener
    {

        public void OnTestEvent(string report)
        {
            Console.WriteLine(report);
        }

    }

Am I doing something wrong? is there a better way to achieve it?

Abel
  • 56,041
  • 24
  • 146
  • 247
Itai Agmon
  • 1,417
  • 13
  • 12

1 Answers1

8

You don't say where you are putting this code, but I am suspecting it's in your test assembly. If so, that's not where it belongs. NUnit engine extensions get installed into the NUnit engine, so they need to be in a separate assembly. Once you have a separate assembly, you need to tell the engine where it is. Currently, you do this by creating a file of type .addins in the same directory as the engine. (You could modify the existing file, but that introduces maintenance problems in the future)

A future release will have an easier way to install addins, but they will continue to be entirely separate from your tests.

A further problem is that you are using TypeExtensionPointAttribute. I didn't notice this originally in your code and it's probably the biggest error so I'm adding this info now.

An "ExtensionPoint" is the thing you are extending. NUnit defines ExtensionPoints, while you create Extenisons to extend them. TypeExtensionPointAttribute is used inside NUnit to define extension points. It's not used by you. You use the ExtensionAttribute to define your extension.

Your extension should be defined something like this:

[Extension(Description = "Test Reporter Extension", EngineVersion="3.4")]
public class MyTestEventListener : ITestEventListener
{
    public void OnTestEvent(string report)
    {
        Console.WriteLine(report);
    }
}

You don't say what version of NUnit you are running. Test Listeners are only supported beginning with version 3.4. The EngineVersion property above is purely documentary at this point, because 3.4 is also the first version to recognize it.

There is a new writeup in the NUnit docs that may be helpful: https://github.com/nunit/docs/wiki/Writing-Engine-Extensions

Alireza
  • 10,237
  • 6
  • 43
  • 59
Charlie
  • 12,928
  • 1
  • 27
  • 31
  • I'm correcting my answer to reflect the fact that the user code is using the wrong Attribute. – Charlie Sep 03 '16 at 17:24
  • 1
    Thanks, I have succeded in writing the addin. Now the problem is that I need it to also run from VS, and my understanding is that is not yet supported. I am looking forward for this enhancement. – Itai Agmon Sep 08 '16 at 14:12
  • 1
    It can probably be made to work on your local machine, by using the NuGet install and creating a .addins file in the package directory with the right relative path in it. But it wouldn't work automatically on package restore, which rules it out in many environments. Beyond that, it will have to wait until the adapter uses a standard engine package, rather than a custom copy of the engine as it now does. – Charlie Sep 09 '16 at 19:34
  • 1
    The `Extension` attribute is defined in the `NUnit.Engine.Api` nuget. – Dejan Dec 07 '17 at 00:25