NUnit extension

729 views
Skip to first unread message

IK

unread,
Jul 12, 2011, 7:19:31 AM7/12/11
to NUnit-Discuss
Hi All i have a question regarding NUnit Extension.

What i am trying to do is write some additional test info to the
database. For that i have created NUnit extension using Event
Listeners.

The problem i am experiencing is that public void
TestFinished(TestResult result) method is being called twice at
runtime. And my code which writes to the database is in this method
and that leaves me with duplicate entries in the database. The
question is: Is that the expected behaviour? Can i do something about
it?
The extension code is below. Thanks.


using System;
using NUnit.Core;
using NUnit.Core.Extensibility;

namespace NuinitExtension
{
[NUnitAddinAttribute(Type = ExtensionType.Core, Name = "Database
Addin", Description = "Writes test results to the database.")]
public class MyNunitExtension : IAddin, EventListener
{
public bool Install(IExtensionHost host)
{
IExtensionPoint listeners =
host.GetExtensionPoint("EventListeners");
if (listeners == null)
return false;

listeners.Install(this);
return true;
}

public void RunStarted(string name, int testCount){}
public void RunFinished(TestResult result){}
public void RunFinished(Exception exception){}
public void TestStarted(TestName testName){}

public void TestFinished(TestResult result)
{
// this is just sample data
SqlHelper.SqlConnectAndWRiteToDatabase("test", "test",
2.0, DateTime.Now);
}

public void SuiteStarted(TestName testName){}
public void SuiteFinished(TestResult result){}
public void UnhandledException(Exception exception){}
public void TestOutput(TestOutput testOutput){}
}
}

Charlie Poole

unread,
Jul 12, 2011, 9:30:58 AM7/12/11
to nunit-...@googlegroups.com
What version of NUnit are you using?

> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.
>
>

IK

unread,
Jul 12, 2011, 9:43:43 AM7/12/11
to NUnit-Discuss
2.5.10.11092

IK

unread,
Jul 12, 2011, 9:44:47 AM7/12/11
to NUnit-Discuss
NUnit version is 2.5.10.11092

Charlie Poole

unread,
Jul 12, 2011, 10:55:31 AM7/12/11
to nunit-...@googlegroups.com
OK, I'll try to replicate the problem.

Charlie

IK

unread,
Jul 12, 2011, 11:35:27 AM7/12/11
to NUnit-Discuss
I have managed to fix the issue by simply removing my extension
assembly from NUnit 2.5.10\bin\net-2.0\addins folder. At the moment
everything works as expected but i am not sure how. I thought that you
have to have the extension/addin assembly inside the addins folder.
I am running tests by opening a solution via NUnit.exe. My extension
project is part of the solution i am testing. Could this be why it is
somehow working?

On Jul 12, 12:19 pm, IK <puf...@googlemail.com> wrote:

Charlie Poole

unread,
Jul 12, 2011, 4:10:14 PM7/12/11
to nunit-...@googlegroups.com
Hi,

Most likely, your addin was being loaded twice.

In order to make it easier to test addins, NUnit searches each test assembly
for addins to be loaded, in addition to searching the addins
directory. Normally,
when you are confident that your addin works, you should remove it from the
test assembly and install it in the addins folder. This makes it available to
all tests that are run using NUnit.

OTOH, if you really only want the addin to apply for a certain project, then
you can leave it in the test assembly and not install it as a permanent addin.

Charlie

IK

unread,
Jul 13, 2011, 3:44:42 AM7/13/11
to NUnit-Discuss
Thanks a lot for the explanation, Charlie. It is much clearer now what
is happening.

Athrun Sun

unread,
May 21, 2012, 2:13:50 AM5/21/12
to nunit-...@googlegroups.com
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?

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?

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?

> To unsubscribe from this group, send email to nunit-discuss+unsubscribe@googlegroups.com.

Charlie Poole

unread,
May 21, 2012, 4:24:20 AM5/21/12
to nunit-...@googlegroups.com
On Mon, May 21, 2012 at 8:13 AM, Athrun Sun <okam...@gmail.com> 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
>> > To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
>> > For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.
>> >
>> >
>
> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/nunit-discuss/-/a730uESbNJUJ.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.

Athrun Sun

unread,
May 22, 2012, 2:55:01 AM5/22/12
to nunit-...@googlegroups.com
Thanks Charlie, I understand now.
I did more research and found out that, if I include project A, B, C, etc. in a nunit test project(say ABC.nunit), the addin implemented in project B works for all the other test projects. I suppose this explains  'NUnit searches each test assembly for addins to be loaded', even if project B contains no tests.
BTW, I'm using NUnit 2.6.0.12051.
>> > To unsubscribe from this group, send email to nunit-discuss+unsubscribe@googlegroups.com.
>> > For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.
>> >
>> >
>
> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/nunit-discuss/-/a730uESbNJUJ.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to nunit-discuss+unsubscribe@googlegroups.com.

Charlie Poole

unread,
May 24, 2012, 7:44:16 AM5/24/12
to nunit-...@googlegroups.com
Yes, this would work because NUnit loads all three as test assemblies,
even though it doesn't actually find tests in B.

charlie
>> >> > nunit-discus...@googlegroups.com.
>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/nunit-discuss?hl=en.
>> >> >
>> >> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "NUnit-Discuss" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/nunit-discuss/-/a730uESbNJUJ.
>> > To post to this group, send email to nunit-...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > nunit-discus...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/nunit-discuss?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nunit-discuss/-/lciduHlpq7gJ.
>
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to
> nunit-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages