Search code examples
nunitnunit-3.0

How to implement ITestEventListener in n-unit


I am trying to implement an custom event listener as shown below

public class CustomEventListener: ITestEventListener
{
    public void OnTestEvent(string report)
    {
        using (StreamWriter writer = new StreamWriter(@"D:\LogFile.txt"))
        {
            writer.WriteLine(report);
        }
    }
}

i have implemented this in a separate project called Extension.csProj. How do i integrate this to n-unit runner? I want this extension to be pickedup automatically for all the tests.

Please let me know how i can do that or any documentation explaining the same


Solution

  • The ITestEventListener interface is used for one type of extension supported by the NUnit TestEngine. Code such as you posted would be part of an extension. Other boilerplate is needed, which you may or may not have.

    Documentation for creating engine extensions is part of the NUnit docs and may be found at https://docs.nunit.org/articles/nunit-engine/extensions/creating-extensions/Index.html

    I suggest reading the full documentation and experimenting a bit. Ask another question if you run into a specific problem.

    One thing the docs do not give you is how or where to install the extension. Unfortunately, that varies depending on what runner you are using and how it was installed. As a start,

    1. Look in your runner installation to see where the engine is located.
    2. Look in the directory of the engine for a file or files of type .addins.
    3. Either edit one of those files or (better) create your own file. There should be a line pointing to where your addin is installed.
    4. If all else fails... yet another question here. :-)