How to use ITestEventListener in NUnit 3?

亡梦爱人 提交于 2020-01-14 02:07:24

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/38749270/how-to-use-itesteventlistener-in-nunit-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!