473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3304
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yashgt | last post by:
Hi, I have installed Visual Studio 2005 Team Edition for Developers, on my Windows XP PC. The exact VS version is 8.0.50727.42. I created a simple Windows application in VB .NET and add a button...
5
by: VvanN | last post by:
hi, fellows I'd like to intruduce a new unit test framework for C++ freely available at: http://unit--.sourceforge.net/ It does not need bothering test registration, here is an example ...
2
by: shuisheng | last post by:
Dear All, I am using visual studipo 2005 (standard version which do not provide unit test tool) to develop some c++ code. I want to do unit test while I am coding. Anybody can suggest me an...
5
by: shuisheng | last post by:
Dear All, I was told that unit test is a powerful tool for progamming. If I am writing a GUI code, is it possible to still using unit test? I have a little experience in using unittest++. But...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
1
by: rich_sposato | last post by:
I released version 2.0 of C++ Unit Test Library. You can download it from SourceForget.Net at http://sourceforge.net/projects/cppunittest/ .. I wrote this unit test library because other unit...
48
by: Ark Khasin | last post by:
Unit testing is an integral component of both "formal" and "agile" models of development. Alas, it involves a significant amount of tedious labor. There are test automation tools out there but...
6
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly...
2
by: =?Utf-8?B?QXJtaW4gR2FsbGlrZXI=?= | last post by:
Hi I've got an unexpected error in a unit test. I want to test a activity from Windows Workflow Foundation (WF). First, I executed the test outside of the activity just in the test-init...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.