NUnit: Accessing the Failure Message in TearDown()
Asked Answered
H

3

3

I'm trying to log the results of automated tests run in NUnit in a small database so that data is easily accessible and more securely logged for various reasons. (There's about ~550 automated tests and running them all can take days)

I already have access to the ending status of the test (Passed/Failed/Error/Cancelled/Skipped etc..) but I'd like to log the extra detail.

I am looking to do this within TearDown().

This is the closest thing I could find, but did not provide me with an answer: https://groups.google.com/forum/?fromgroups=#!msg/nunit-discuss/lXxwECvpqFc/IbKOfQlbJe8J

Ideas?

Headmaster answered 15/1, 2013 at 15:23 Comment(1)
I'm running selenium tests through NUnit's GUI (as opposed to the console version), the tests themselves inherit a base test class that holds the setup/teardown methods and anything of global value to the tests.Headmaster
P
4

I believe you'll be able to get the information you need with NUnit EventListeners. I haven't used these myself, but I've had them bookmarked to do something similar to what you're trying to accomplish.

Here's the interface you'd be working with. Hopefully your TearDown method would be called just before TestFinished, but I can't verify this.

public interface EventListener
{
    void RunStarted(string name, int testCount );
    void RunFinished(TestResult result);
    void RunFinished(Exception exception);
    void TestStarted(TestName testName);
    void TestFinished(TestResult result);
    void SuiteStarted(TestName testName);
    void SuiteFinished(TestResult result);
    void UnhandledException(Exception exception);
    void TestOutput(TestOutput testOutput);
}
Pox answered 15/1, 2013 at 19:37 Comment(2)
Firstly thanks for showing me this (i'd +1 but my rep is too low), a more in-depth version is simple-talk.com/dotnet/.net-tools/… unfortunately, I have no idea how to properly implement this so that my methods are called but then they can very simply update a "errorMessage" property.Headmaster
Ok, thanks to your answer I found this: https://mcmap.net/q/1633648/-nunit-extension. Chucking an class that implements this interface together and only using TestStarted and TestFinished sufficed for my needs. These are called just before SetUp and after TearDown so any extra logging you want to do should also be done in that class.Headmaster
H
1

For those that want some skellie code:

[NUnitAddinAttribute(Type = ExtensionType.Core,
Name = "Database Addin",
Description = "Writes test results to the database")]
public class MyExtension :IAddin, EventListener
{
//some private attributes to hold important data

//you must provide the Install method
    public bool Install(IExtensionHost host)
    {
        //I also built my connection string in here
        IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
        if (listeners == null)
             return false;

        listeners.Install(this);
        return true;
    }

//you must also provide all the event handlers, 
//but they don't have to actually do anything if they are not used.
//e.g.

    public void TestStarted(NUnit.Core.TestName testName)
    {
        //This saved the start time of the test
        _start =  DateTime.Now;
    }

    public void TestFinished(NUnit.Core.TestResult result)
    {
        //LogTest connected to the databse and executed a proc to 
        //insert the log, was quite simple
        LogTest((result.Message == null? "" : result.Message),
            result.ResultState,
            result.Name,
            _start,
            DateTime.Now);
    }

    public void TestOutput(NUnit.Core.TestOutput testOutput)
    {
         //this is one of the unused event handlers, it remains empty.
    }
    //etc..

}
Headmaster answered 17/1, 2013 at 9:41 Comment(0)
M
1

NUnit 3.0 has these details contained inside of TestContext.CurrentContext.~

Caution: In the event that you have the VS test adapter included as an extension, using the event handler will cause the tests to run twice. Once for the extension and once for the including dll required for implementing the event handler.

Mabelmabelle answered 15/2, 2016 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.