Question

I'd like to log some contextual information when tests fail (like capturing screenshots). Is there any extensibility point in NUnit framework where i can do it? Ideally it would be:

    [TearDown]
    public void Down(AssertionException ex) {}

but we don't have it. I tried to create an add-in but don't know how to register it in run-time:

[NUnitAddin]
public class UITestCase : TestCaseBase, NUnit.Core.EventListener, NUnit.Core.Extensibility.IAddin {
   .............
    public void UnhandledException(Exception exception){}

    public bool Install(NUnit.Core.Extensibility.IExtensionHost host) {
        IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
        listeners.Install(this);
        return true;
    }

}

UITestCase is my base class for all UI tests. Install() method is just not get called. Any tips would be appreciated.

Update: for an add-in to be registered tests have to run with NUnit runner (in my case it was TestDriven.Net plugin for VS) and EventListener members are get properly called. The problem is that they all called after TearDown/TestFixtureTearDown methods (my context is lost).

Tried to use TestContext.CurrentContext.Result in my TearDown but accessing either State or Status property throws NullReferenceException :(

Was it helpful?

Solution 3

After upgrading to NUnit 2.6 TestContext.CurrentContext started working as expected.

OTHER TIPS

If your intention is to run tests of your application in action, NUnit is not the best choice, it just wasn't intended to be used that way. NUnit is for unit tests, not integration tests, which would test how your UI integrates with logic and data. When unit tests are running (including those under nunit), there is no screenshot to be captured - the test runner directs output to a log file which contains everything you could want to know about the test, including exceptions.

If you are trying to do UI testing, I recommend Watin for WebApp tests, and White for WPF/Winforms app tests.

I think that the IAddin approach has the most promise, although the EventListeners might not by the appropriate extension point for your needs. Have you tried the TestCaseBuilders or TestDecorators extentions?

For example, the TestDecorators documentation states:

Purpose

TestDecorators are able to modify a test after it has been constructed.

Extension Point

Addins use the host to access this extension point by name:

IExtensionPoint testDecorators = host.GetExtensionPoint( "TestDecorators" ); Interface

The extension object passed to Install must implement the ITestDecorator interface:

public interface ITestDecorator     
{       
    Test Decorate( Test test, MemberInfo member );  
} 

The Decorate method may do several things, depending on what it needs to accomplish:

  1. Return test unmodified
  2. Modify properties of the test object and return it
  3. Replace test with another object, either discarding the original or aggregating it in the new test.

That sounds like a good place to try wrapping the test with custom code.

I think you can use try catch inside your test method to catch exception and then do some custom action like capturing screenshot inside catch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top