NUnit Addins That Work With Resharper

If you’ve tried to create an NUnitAddin that works with Resharper you quickly found that it simply doesn’t work.  In fact, it’s been confirmed that Resharper does not currently support NUnit EventListener addins.  While this is true, I’ve found a work around that works very nicely.

Instead of adding an EventListener directly, add an ITestDecorator first.

    [NUnitAddin]
    public class CustomDecorator:IAddin, ITestDecorator
    {
        public bool Install(IExtensionHost host)
        {
            var testDecorators = host.GetExtensionPoint("TestDecorators");
            if (testDecorators == null) return false;
            testDecorators.Install(this);
            return true;
        }
 
        public Test Decorate(Test test, MemberInfo member)
        {
            if (test.GetType() == typeof(NUnitTestMethod) &&
                ((NUnitTestMethod)test).Method.
                    GetCustomAttributes(typeof(IgnoreAttribute), true).
                    Length == 0)
            {
                return new TestMethodWrapper((NUnitTestMethod) test);
            }
            return test;
        }
 
    }

In this example I only wrap tests that are of the type ‘NUnitTestMethod’. I also found that methods that were marked with Ignore were not being ignored, so I added an attribute check.

    public class TestMethodWrapper : NUnitTestMethod
    {
 
        private Test _test;
 
        public TestMethodWrapper(NUnitTestMethod testMethod) :
                     base(testMethod.Method)
        {
            _test = testMethod;
        }
 
        public override TestResult Run(EventListener listener,
                                                   ITestFilter filter)
        {
            return base.Run(new CustomEventListener(listener), filter);
        }
 
    }

The ‘TestMethodWrapper’ wraps Resharper’s EventListener with my own ‘CustomEventListener’.

    public class CustomEventListener:EventListener
    {
        private EventListener _eventListener;
 
        public CustomEventListener(EventListener eventListener)
        {
            _eventListener = eventListener;
        }
 
        ...
 
        public void TestFinished(TestResult result)
        {
            if (_eventListener != null)
                _eventListener.TestFinished(result);
            //do something
        }
 
       ...
    }

Since this ‘EventListener’ could be called outside of Resharper where there may not be an preexisting ‘EventListener’, I wrapped my delegating call with a null check.

If you’re only running your tests in the NUnit GUI console because your ‘EventListner’ is not run by Resharper, I hope this work around helps you run your tests in Visual Studio again.

5 comments

  1. Do you have example code for a nunit test that will have the listener attached to it. I am trying implement this but I am unsuccessful and there’s damn near no documentation on how to implement this correctly.

  2. To attach the listener to your tests you need to create a DLL containing these new NUnitAddin classes and add it to your [root NUnit]/bin/addins directory.

  3. Hi,
    Your example looks good but it is not working for me.
    Can you share your working code example?

  4. This *so* nearly works for me…. I have some tests with attributes like [TestCase(36, 14930352)] and [Test, Sequential]. These don’t seem to get passed to the Decorate method at all, so don’t get hooked. (I’m on version 2.6.3)

  5. Also you need to change default settings in resharper:
    http://i.imgur.com/2qT0y2U.png
    Options -> Tools -> Unit Testing -> NUnit
    Load NUnit Addins -> Always
    and copy dll with Addin to C:\Users\…\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12\addins

    Also your dll with tests (not with addin) must not contain NUnit.AddinsDependencies package

Comments are closed.