detecting running under UNIT TEST

I

Ing. Davide Piras

Hi there,
I use Visual Studio 2005, c# projects...

I like the new unit test framework very much, it helps a lot mantaining and
growing up a project with the test driven paradigm, that's so good!

I'm looking for a way to detect if the program is running under the TEST
enviroment or running normally, let's say I need something like

#if TESTING

do some stuff...

#endif

or, I don't know what else... something that comes from the framework, I
don't want to set the TESTING by myself like DEBUG / TRACE in the compiler
configuration options...

any idea ?

Thanks, Davide.
 
M

Markus Becker

Ing. Davide Piras said:
I'm looking for a way to detect if the program is running under the TEST
enviroment or running normally, let's say I need something like

#if TESTING

do some stuff...

#endif

You don't want to do that, because it completely defeats the purpose
of the tests. You'll introduce bugs that the testing framework will
not find.

Markus
 
I

Ing. Davide Piras

Thank'you Markus, I think you are right, but probably there are some cases
where a swithc could help...

we develop a web application, for instance, we test the debug version in the
local machine then we deploy the release version on the remote server, the
remote server has SMTP on 127.0.0.1, the local machine has not

so, I would like to skip only the last code line (the mail sending action)
in the test case and still return true so the test is passed, the critical
code that could crash and should be tested is before!!!!

.... even if I'd a mail server on my computer, there is not point to send
email while testing !!

any idea ?

thanks, Davide.
 
P

PS

Ing. Davide Piras said:
Thank'you Markus, I think you are right, but probably there are some cases
where a swithc could help...

we develop a web application, for instance, we test the debug version in
the local machine then we deploy the release version on the remote server,
the remote server has SMTP on 127.0.0.1, the local machine has not

so, I would like to skip only the last code line (the mail sending action)
in the test case and still return true so the test is passed, the critical
code that could crash and should be tested is before!!!!

... even if I'd a mail server on my computer, there is not point to send
email while testing !!

any idea ?

The whole idea of testing is that making your code testable is what improves
the quality of your code. Without seeing the code I can only guess at what
is happening. You seem to be accessing some SMTP object that sends an email
and return true to indicate success. Make this SMTP object a parameter and
pass in a mock SMTP object that does nothing when SendMail() method is
called and returns true.

PS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top