Try - catch in an Unit test

B

batvanio

Hi,

I am writing unit tests in VS2005 and am having the following problem:
I am trying to test a timeout property of one of my methods. This
timeout exhibits itself in an exceptioin - i.e. I am sending a command
to a hardware device, and if it does not answer in the timeout defined
(i.e. in 2 sec), my software fires an exception.
Now, I am writing an unit test in which I start a timer and I send the
command. Note that I DO expect an exception, so i send the command in a

try{} statement, and then I have a catch{...} in which i am trying to
trap the exception. In this same catch I am measuring the timer to make

sure my software did wait for 2 seconds before firing the exception.


timer.Start();
try
{
command.send()


}


catch
{
//I DO expec this but this line never gets hit in the unit test
timer.Stop();
//measure timer


}


Now the problem - Whenever the exception happens, the unit test fails
immediately (even though I DO expect that exception to happen) so the
catch never gets hit.
On the other hand, if I include [ExpectedException (....)] attribute,
the unit test always passes as soon the exception gets fired so it agan

never goes to the catch{}.

Is there any way to actually get to actually execute the catch
statement in a VS2005 unit test? I need to make use of the code in the
catch statement. I tried to find info on this on MSDN but with no
success.


Thanks ,


Ivan
 
B

batvanio

Never mind - i was catching too specific of exception - if you do

catch (Exception e)

it seem to work...

Thanks,

Ivan
 

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