Captures statements in NUnit

I want to record some contextual information on failed tests (for example, capturing screenshots). Is there any extension point in the NUnit framework where I can do this? Ideally, this would be:

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

but we don’t have it. I tried to create an add-in, but I don’t know how to register it at runtime:

 [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 user interface tests. Install() method is simply not called. Any advice would be appreciated.

Update: for the add-in to be registered, must run with the NUnit runner (in my case it was TestDriven.Net for VS) and the EventListener members will get the proper call. The problem is that they are all called after the TearDown / TestFixtureTearDown methods (my context is lost).

Tried to use TestContext.CurrentContext.Result in my TearDown, but getting access to the State or Status NullReferenceException : (

+6
source share
4 answers

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

+2
source

If you intend to run tests of your application in action, NUnit is not the best choice, it is simply not intended to be used that way. NUnit is unit tests, not integration tests, which will check how your interface integrates with logic and data. When unit tests are performed (including for nunit), there is no screenshot - the test runner sends the output to a log file that contains everything you might know about the test, including exceptions.

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

+3
source

I think the IAddin approach is most significant, although EventListeners may not match the appropriate extension point for your needs. Have you tried TestCaseBuilders or TestDecorators extensions

For example, the TestDecorators documentation reads:

goal

TestDecorators can modify the test after it has been built.

Expansion 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 can perform several actions, depending on what it needs to perform:

  • The reverse test is unmodified
  • Change the properties of the test object and return it
  • Replace the test with another object, either discarding the original or aggregating it in a new test.

This seems like a good place to try wrapping the test with custom code.

+2
source

I think you can use try catch inside your test method to catch an exception, and then perform some custom actions like capturing screenshots inside catch.

0
source

Source: https://habr.com/ru/post/907438/


All Articles