Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit addin - do I have to include addin implementations in each test projects?

Tags:

load

nunit

add-in

I've found a useful post recently in here: NUnit extension

However my question is still not answered.

First of all, what is a 'test assembly' anyway?

Second, could someone give me a more detailed explanation of 'NUnit searches each test assembly for addins to be loaded'?

For example, I have two projects in my VS2010 solution, say, project A and project B. A is a test project(contains '[Test]' inside), B is an NUnit addin project(contains addin installer, EventListener interface implementations, etc. inside), and, A references B. Does this work? Will the addin be called?

If not, I assume it means that I must have the various .cs files(which implements the NUnit addin) directly included in project A, rather than have them placed into a separate project and reference it in test project. Is that what you mean?

If so, another problem raised, that, when I have project C, D, E... which are also test projects, I have to include those various .cs files(which implements the NUnit addin) in each test project?

like image 684
Bruce Sun Avatar asked May 21 '12 06:05

Bruce Sun


2 Answers

For the Add-In to run, one of two conditions must be satisfied:

  • The Add-in is a compiled .dll which is placed in the 'AddIns' folder.
  • The Add-in code is in the same Assembly as the tests.

There is a way to have the bulk of the Add-in code in a different Assembly, however, and to have a small shim class in your Test Assembly which allows NUnit to find it, for example:

In Addin Assembly

public class CustomEventListener : IAddin, EventListener
{
    public bool Install(IExtensionHost host)
    {
        IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
        if (listeners == null)
            return false;

        listeners.Install(this);
        return true;
    }

    ........ <Implemented Interfaces> .......

}

In Test Assembly

Add this class anywhere you like:

[NUnitAddin]
public class MyAddin : CustomEventListener { }

NUnit will spot the NUnitAddin attribute and invoke the code in the base CustomerEventListener code, even though it is in a different assembly.

like image 80
Holf Avatar answered Oct 25 '22 08:10

Holf


This is the answer Charlie provided on google groups. Many thanks to Charlie! https://groups.google.com/forum/?fromgroups#!topic/nunit-discuss/yTKRKf2APLI

Re: [nunit-discuss] Re: NUnit extension

On Mon, May 21, 2012 at 8:13 AM, Athrun Sun wrote:

Hi Charlie,

Could you give me a more detailed explanation of 'NUnit searches each test assembly for addins to be loaded'?

For example, I have two projects in my VS2010 solution, say, project A and project B. A is a test project(contains '[Test]' inside), B is an NUnit addin project(contains addin installer, EventListener interface implementations, etc. inside), and, A references B. Does this work? Will the addin be called?

In that situation, no. B is not a test assembly.

If not, I assume you mean that I must have the various .cs files(which implements the NUnit addin) directly included in project A, rather than have them placed into a separate project and reference it in test project. Is that what you mean?

Exactly

If so, another problem raised, that, when I have project C, D, E... which are also test projects, I have to include those various .cs files(which implements the NUnit addin) in each test project?

This feature is provided to allow testing of extensions under development, so it's not really a problem with needing to use the extension in multiple assemblies. In fact, if you were to duplicate the code in two assemblies, I imagine NUnit would try to register two different addins, with potentially surprising results. :-)

For production use, you should install the addin in the addins folder in the normal way. In addition, it's best not to reference the addin assembly from your code. Although this may not always cause a problem, it sometimes does lead to the addin being loaded twice.

Charlie - show quoted text -

To view this discussion on the web visit https://groups.google.com/d/msg/nunit-discuss/-/a730uESbNJUJ. - show quoted text -

like image 25
Bruce Sun Avatar answered Oct 25 '22 06:10

Bruce Sun