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?
For the Add-In to run, one of two conditions must be satisfied:
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:
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> .......
}
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.
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 -
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