Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching asserts in 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.

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 :(

like image 340
UserControl Avatar asked Feb 01 '12 22:02

UserControl


People also ask

What is the use of Assert class in NUnit?

NUnit Assert class is used to determine whether a particular test method gives expected result or not. In a test method, we write code the check the business object behavior. That business object returns a result. In Assert method we match the actual result with our expected result.

Can NUnit assertions be used with C# & selenium?

It depends on what the test code should do after a certain condition is met. By the end of this article, you will get in-depth insights about NUnit asserts and how these asserts can be used with C# & Selenium. I will walk you through the Assertions’ basics and when assertions should be used to get started.

How many NUnit asserts can be used in one Test?

However, many developers have multiple asserts in one test, but that is not a good programming practice since the tests following the first NUnit assert do not get executed. Hence, it is recommended to have only one NUnit assert per test. Before NUnit 2.4, Classic Model was used for using NUnit asserts.

What is throws in NUnit?

Exception Asserts (NUnit 2.5) The Assert.Throws method is pretty much in a class by itself. Rather than comparing values, it attempts to invoke a code snippet, represented as a delegate, in order to verify that it throws a particular exception.


3 Answers

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.

like image 142
RyanR Avatar answered Oct 01 '22 03:10

RyanR


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.

like image 30
Addys Avatar answered Oct 01 '22 03:10

Addys


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

like image 31
UserControl Avatar answered Oct 01 '22 01:10

UserControl