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
:(
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.
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.
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.
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.
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:
- Return test unmodified
- Modify properties of the test object and return it
- 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.
After upgrading to NUnit 2.6 TestContext.CurrentContext
started working as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With