Captura de afirmaciones en NUnit

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.

Actualizar: 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 después de TearDown/TestFixtureTearDown methods (my context is lost).

Intenté usar TestContext.CurrentContext.Result in my TearDown but accessing either State or Status property throws NullReferenceException :(

preguntado el 01 de febrero de 12 a las 22:02

4 Respuestas

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 Blanco for WPF/Winforms app tests.

Respondido 13 Feb 12, 04:02

There's also Coded UI, which is in-the-box with VS2010 Premium/Ultimate. - Daniel Mann

I do UI testing with Selenium that generates NUnit compatible c# code. However what i want is framework agnostic. I just need to catch test failures. No big deal especially when NUnit has the means for it (extension points and TestContext). - Control de usuario

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:

Propósito

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.

Respondido 13 Feb 12, 03:02

Thanks, but i can't see how it can help. Looked at the members of NUnit.Core.Test and see no appropriate points to get test result :( - Control de usuario

After upgrading to NUnit 2.6 TestContext.CurrentContext comenzó a trabajar como se esperaba.

Respondido el 20 de junio de 12 a las 18:06

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.

Respondido 02 Feb 12, 09:02

Oh no. When you have few thousand tests it's not an option. - Control de usuario

Can you please tell me whether it's a data driven test or you have thousand different test cases. - Pritam Karmakar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.