468,537 Members | 1,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

Try - catch in an Unit test

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

Nov 16 '06 #1
1 3017
Never mind - i was catching too specific of exception - if you do

catch (Exception e)

it seem to work...

Thanks,

Ivan

ba******@gmail.com wrote:
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
Nov 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by VvanN | last post: by
2 posts views Thread by shuisheng | last post: by
5 posts views Thread by shuisheng | last post: by
176 posts views Thread by nw | last post: by
1 post views Thread by rich_sposato | last post: by
48 posts views Thread by Ark Khasin | last post: by
6 posts views Thread by Vyacheslav Maslov | last post: by
2 posts views Thread by =?Utf-8?B?QXJtaW4gR2FsbGlrZXI=?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.