How to invoke NUnit programmatically

1,286 views
Skip to first unread message

Mahesh Narayan

unread,
Apr 21, 2011, 5:03:49 AM4/21/11
to NUnit-Discuss
Hi,
I am a test automation lead in my organization We have developed a
custom automation framework to do testing of our products. The product
and automation framework are both in C#.

Currently, we have coded all our test cases as C# classes that get
invoked through NUnit. The classes have the [TestFixture] attribute
and also the Execute() function that NUnit recognizes and invokes.

We will be moving to a new test runner different than NUnit shortly.
We want to minimize our code changes. Hence, I would like to see
whether I can still launch NUnit programmatically behind the scene
when my new test runner executes.

Once NUnit is launched (with appropriate command line arguments), my
existing classes would still work because they are NUnit compatible.

Please let me know how I can do this. It is urgent. Does Nunit have a
list of APIs that I could invoke?

Thanks and Regards,
Mahesh.


Charlie Poole

unread,
Apr 21, 2011, 10:40:02 AM4/21/11
to nunit-...@googlegroups.com
Hi Mashesh,

On Thu, Apr 21, 2011 at 2:03 AM, Mahesh Narayan <naraya...@gmail.com> wrote:
> Hi,
> I am a test automation lead in my organization We have developed a
> custom automation framework to do testing of our products. The product
> and automation framework are both in C#.
>
> Currently, we have coded all our test cases as C# classes that get
> invoked through NUnit. The classes have the [TestFixture] attribute
> and also the Execute() function that NUnit recognizes and invokes.

I don't understand. NUnit does not recognize or invoke any function
called "Execute" on the user fixture class. It calls methods marked
with particular attributes.

> We will be moving to a new test runner different than NUnit shortly.
> We want to minimize our code changes. Hence, I would like to see
> whether I can still launch NUnit programmatically behind the scene
> when my new test runner executes.

When you say "launch NUnit" what parts of NUnit are you wanting
to launch? I guess you don't want to run nunit.exe or nunit-console.exe,
but there are various levels at which you could consider using NUnit,
since it is essentially a layered application.

Perhaps you could describe what your own runner will do and what you
would like NUnit to do.

> Once NUnit is launched (with appropriate command line arguments), my
> existing classes would still work because they are NUnit compatible.

OK... now I see "command-line arguments" so I'll contradict myself.
Apparently you want to run either nunit-console.exe or nunit.exe
programmatically. But I won't change the prior statements because
it would still help if you explained a bit more

> Please let me know how I can do this. It is urgent. Does Nunit have a
> list of APIs that I could invoke?

OK... more confusion. If you are simply launching the executable, no
API is needed - or possible. If you want to make use of some NUnit
internals by using (for example) nunit.core.dll, then the need for an
API makes sense. However, these two approaches would appear to
be mutually exclusive. Can you explain further?

One small comment. NUnit currently has no formally supported API
for external use, so whatever you do is adhoc and could be changed
in a future release. Obviously, some things are less likely to change
than others and I can help advise you on that. One of the main
goals of NUnit 3.0 is to provide a public API for using NUnit.

Sorry for my confusion. I think it's because you have a certain way
of using NUnit in your mind and I am thinking of three or four
different ways you might do it. If you can give more details, we'll
try again!

Charlie
> Thanks and Regards,
> Mahesh.
>
>
> --
> 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.
>
>

Mahesh Narayan

unread,
Apr 21, 2011, 11:15:30 AM4/21/11
to NUnit-Discuss
Thank you for your reply.

Currently, we have haev something that looks like this:

namespace CardiacWorkFlows2D
{
[TestFixture]
public class NUnitTests
{
[Test]
public void Nunit_NEMO_STP_A1_11()
{
NEMO_STP_A1_11 test = new NEMO_STP_A1_11();
test.Execute();
}
}
}

And we have corresponding test classes that are defined like this:
namespace CardiacWorkFlows2D
{
public class NEMO_STP_A1_11
{
public static float ejectionFraction;

public void Execute()
{
.............
}
}
}


All our classes and the NUnit TestFixture class get compiled into a
DLL. We then launch the NUnit GUI, load the DLL as a project, select
the classes in the DLL that we want to run, run the tests.

Now, we have been asked to make our classes compatible with another
test runner as well, in addition to NUnit. Normally this would require
restructuring our code to make it readable by the new test runner.
However, when the new test runner runs, in its Execute function (or
equivalent function), if I could invoke NUnit programmatically and
pass my DLL and my classes as arguments, then the new test runner
would just become a wrapper that internally invokes NUnit
programmatically to execute my tests.

I need to know how this can be done. Is there any API that I can call
that would be the same as launching the NUnit GUI, selecting tests,
and clicking RUN, except all this is now being done programmatically?

Hope this makes it a bit more clear.

Regards,
Mahesh.
On Apr 21, 7:40 pm, Charlie Poole <nunit...@gmail.com> wrote:
> Hi Mashesh,
>
> > For more options, visit this group athttp://groups.google.com/group/nunit-discuss?hl=en.- Hide quoted text -
>
> - Show quoted text -

Mahesh Narayan

unread,
Apr 21, 2011, 11:23:50 AM4/21/11
to NUnit-Discuss
" need to know how this can be done. Is there any API that I can call
that would be the same as launching the NUnit GUI, selecting tests,
and clicking RUN, except all this is now being done
programmatically?".........I mean launching NUnit, not necessarily the
NUnit GUI....I just want to be able to start Nunit, pass my DLL and
classes are arguments, and then ask it to execute the tests in the
arguments one by one.
> > > For more options, visit this group athttp://groups.google.com/group/nunit-discuss?hl=en.-Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -

Charlie Poole

unread,
Apr 21, 2011, 2:06:26 PM4/21/11
to nunit-...@googlegroups.com
Hi Mahesh,

Your example makes it clearer to me.

Here are some options you might consider, from highest level to lower level:

1. Just launch a process running nunit-console.exe, passing in command line
arguments that tell it what assembly to load and what tests to
run. For example:

nunit-console.exe mytests.dll -run="testfixture1,testfixture2,testfixture3"

I suggest experimenting with doing this manually first before trying it
programmatically.

You can also use nunit.exe this way, but with no ability to select
tests to run.

2. Use nunit-console-runner.dll and execute Runner.Main, passing in an array of
arguments. This is the same as running nunit-console.exe except you don't
have to launch a separate process.

NOTE: Both of the above will produce the normal nunit-console text output. If
you want control of the output yourself, you most go to a lower level.

3. Use nunit-gui-runner.dll to load the assembly tests in the gui. Execute
AppEntry.Main, passing in the path to your assembly and any other args.
In order to select tests, use Services.TestLoader to get a reference to the
TestLoader and call methods on it to run the tests. You will need
to construct
and pass in a filter in order to select specific tests. See the
source code for
more details.

4. Rather than using the Gui, use TestLoader directly yourself. You will have to
perform some initialization of the services first - see how the
console runner
does it in ConsoleUi.cs as a start.

5. Select some lower level runner and use it directly. For example,
use TestDomain
to run in-process in a separate AppDomain. If you later wanted to
run the tests
in a separate process, then you would have to change your code to use a
ProcessRunner, rather than just changing an option in the TestPackage as
is done when you use TestLoader.

All runners implement the TestRunner interface, which is fairly
straightforward.

Hope this helps you.

Charlie

Reply all
Reply to author
Forward
0 new messages