Re: Am I Running Under NUnit?

"Charlie Poole" <[email protected]>
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <007d01c89f2e$61654d40$6401a8c0@ferrari>
Hi Mike,


I would think that a conditional define in your code would be simpler.

 

#if NUNIT

// do my logging

#endif

 

Why would you want to carry test code into production anyway?  I can think
of some reasons in our environment where we might, but I'd like to hear you
articulate yours.

 

Makes sense to me.

 

Not that your idea doesn't have merit.  It may be that some mechanism for
identifying whether or not code is running under NUnit would make sense to
add to NUnit version 3.

 

Agreed.

 

Mike

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Ron Gross
Sent: Tuesday, April 15, 2008 11:23 AM
To: Charlie Poole
Cc: [email protected]
Subject: Re: [Nunit-users] Am I Running Under NUnit?

 

What I mean is this: I would like NUnit to expose some boolean method or
property that simply says if your main core that loops through the unit
tests has begun.
I'm sure you have method in your code similar to:

void RunSelectedTests()
{
  ..
  foreach (test in unit tests)
  {
    ...
    test.Run();
  }
}

What I want is a slight addition to this method:

void RunSelectedTests()
{
  ..
  _runningUnderNUnit = true;
  foreach (test in unit tests)
  {
    ...
    test.Run();
  }
}

With the variable _runningUnderNUnit exposed through a read-only property.
Seems rather easy to implement.

I'm referencing NUnit from some of my production code, because there are
certain things (logging, for example) that I want to do only if I'm in the
context of a unit test, and my current method of detecting if I'm in NUnit
or not, from this
<http://geekswithblogs.net/ajohns/archive/2004/08/05/9389.aspx> post,
requires a reference. I'm aware that there are other ways to accomplish
this, configuration for example, but the above approach seems simpler.

On Tue, Apr 15, 2008 at 8:49 PM, Charlie Poole <[email protected]>
wrote:

Hi Ron,

 

Our conversation appears to have fallen off the list - I put us back on it.

 

I'm confused... You already reference NUnit from your production code?

I'm assuming that's what we are talking about - if you want to figure out

whether your tests are running under NUnit or some other runner, that's

a different - perhaps a simpler - matter.

 

As far as being forced to use a hack: the entire notion seems like a

hack to me, and NUnit can't do anything to improve it. We can't even

hide the hack for you, since we have no general way to determine if

some arbitrary code is running under NUnit - and it would have to be

a general solution if NUnit implemented it, while you can simply do

something that works for your own particular case.

 

In fact, NUnit's lack of knowledge of where threads originate is already

a source of difficulty for us - one I hope to solve as part of the 3.0
rewrite.

 

If I'm misunderstanding something here, please let me know, but this

does not sound like a service NUnit can readily provide.

 

Charlie

 

I

 

  _____  

From: Ron Gross [mailto:[email protected]] 

Sent: Monday, April 14, 2008 11:13 PM


To: Charlie Poole
Subject: Re: [Nunit-users] Am I Running Under NUnit?

 

So you think it's better for me to do this hacks than for you to expose a
method?
After all, I am dependant on NUnit and reference it anyway.

Only this way, I'm forced to these ugly hacks.

On Tue, Apr 15, 2008 at 12:52 AM, Charlie Poole
<[email protected]> wrote:

Yes, your original method suffers from the same problem if threads are
involved.

 

If we exported such a function, then people would have to reference it,
making their apps 

dependent on NUnit - so no. :-)

 

Another possibility is to look at the entry assembly and see if it's
nunit.exe or nunit.console.

However, that may not work in a secondary AppDomain - I'm not sure.

 

Charlie

 

 

  _____  

From: Ron Gross [mailto:[email protected]] 
Sent: Monday, April 14, 2008 1:13 PM
To: Charlie Poole
Subject: Re: [Nunit-users] Am I Running Under NUnit?

The 2nd way is not perfect, because if the call is made from another thread
then its stack will not have any NUnit frames.
Is there a chance such a function can be exported from NUnit itself in some
future release?

On Mon, Apr 14, 2008 at 10:15 PM, Charlie Poole
<[email protected]> wrote:

Hi Ron,

 

The code is wrong on several levels:

 

1) Tactically, because it will fail if your test references a different
version of the framework than the one it

was built against.

 

2) Strategically, because the presense of a Test attribute doesn't have to
mean your test is running under

NUnit. However, it may be so for a particular user who never calls the tests
any other way.

 

There are two simple ways to test whether you are running under NUnit... 

 

One is to look at the friendly name of the current appdomain, which is
"domain-<project>" where <project> 

is the name of the assembly or project being executed. Obviously, this is
subject to name clashes.

 

The other - and more accurate - is to search the stack for the name of the
nunit.core assembly.

 

Charlie

 

  _____  

From: [email protected]
[mailto:[email protected]] On Behalf Of Ron Gross
Sent: Monday, April 14, 2008 7:50 AM
To: [email protected]
Subject: [Nunit-users] Am I Running Under NUnit?

How can I implement a function AmIRunningUnderNUnit() ?
I have a logger, and I want to Log only if I'm running under NUnit.

This is a suggested solution:
http://geekswithblogs.net/ajohns/archive/2004/08/05/9389.aspx

But it doesn't work if the test spawns different threads and the function is
called from there.

Thanks,
Ron


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javao
ne
_______________________________________________
Nunit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nunit-users

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

_______________________________________________
Nunit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nunit-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.