Methods of the class inherited from ITestListener are not started. NUnit

Viewed 77

I have an implementation of the listener interface and I want to save a screenshot of the web driver, as well as the xml of the test itself.

public class TestListener : ITestListener
{
    public void TestFinished(ITestResult result)
    {
        try
        {
            string fileName;
            if (!Directory.Exists("Logs")) Directory.CreateDirectory("Logs");

            if (Directory.GetFiles(@"Logs\").Length == 0)
            {
                fileName = @"Logs\Log_0.xml";
            }
            else
            {
                fileName = @"Logs\Log_" + System.Convert.ToString(
                    Directory.GetFiles(@"Logs\").Select(item => System.Convert.ToInt32(item.Split('_')[1]))
                    .OrderByDescending(item => item).First() + 1) + ".xml";
            }

            using (StreamWriter fstream = new StreamWriter(fileName))
            {
                fstream.Write(result.ToXml(true).OuterXml);
            }
        }
        catch {}

        if (result.ResultState.Status == TestStatus.Failed)
        {
            try
            {
                Screenshot screenshot = ((ITakesScreenshot)Driver.DriverInstance.GetInstance()).GetScreenshot();

                if (!Directory.Exists("Screenshots")) Directory.CreateDirectory("Screenshots");
                if (Directory.GetFiles(@"Screenshots\").Length == 0)
                    screenshot.SaveAsFile(@"Screenshots\Screen_0.jpg");
                else
                {
                    string fileName = @"Screenshots\Screen_" + System.Convert.ToString(
                        Directory.GetFiles(@"Screenshots\").Select(item => System.Convert.ToInt32(item.Split('_')[1]))
                        .OrderByDescending(item => item).First() + 1) + ".jpg";

                    screenshot.SaveAsFile(fileName);
                }
            }
            catch {}
        }
    }

    ....

}

and I run a illustration test to show to see that it works

    [Test]
    public void test()
    {
        Assert.IsTrue(false);
    }

but don't push a event for listener. NUnit v3.12.0

1 Answers

As you discovered, the NUnit Framework has an interface ITestListener, which it uses to communicate events about tests. However, it isn't something you can create in your tests because you have no way to cause NUnit to call your methods with that interface.

You have probably confused this interface with ITestEventListener, which is an interface provided by the NUnit engine. The engine, of course, is a different piece of software. It's the software, which runs tests written against the NUnit framework. The engine allows several types of extensions to be created and one of those is, in fact, a test event listener extension, implementing ITestEventListener.

However, you can't implement an extension by including its code in your tests. (See note) Extensions are implemented separately and must be installed into the engine you are using... i.e. the engine that came with your test runner.

I can't write a complete how-to here for implementing engine extensions. I'll refer you to the docs for that...

https://docs.nunit.org/articles/nunit-engine/extensions/creating-extensions/Event-Listeners.html

If you look at the index on that page, you'll also see there is info about both framework and engine extensibility. Once you have absorbed it, I suggest another question for any problems you may run into.

Note: I wish it were possible to just include the class in your tests and have it added as a sort of adhoc extension! However, there are architectural reasons why that's next to impossible... for one thing, your tests may be targeting a completely different runtime from the engine itself!

Related