473,725 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 [ExpectedExcepti on (....)] 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 3334
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 [ExpectedExcepti on (....)] 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
1369
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 and its handler. I then tried to add a Unit test case for the handler, using Test>New Test>Unit Test Wizard. The handler for the button is not seen the the list of functions available to be unit tested. The Form1 class itself is not listed. On a...
5
2501
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 // --- begin code ---
2
1874
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 easy-to-use tool for unit tests? I appreciate your suggestion. Bests,
5
6525
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 I can not work out a way to use it to test GUI code. Thanks a lot!
176
8390
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
1
1800
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 test frameworks always lacked features I considered important. Some only provided output in certain formats, and I had no easy way to choose my own format. Most provided no protection against exceptions. Only a few were useful for unit-testing...
48
2502
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 from what limited exposure I've had, they are pricey, reasonably buggy, and require compiler/target adaptation. Out of my frustration with two out of two of them came my own. Its instrumentation approach is based solely on profound abuse of the C
6
5686
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 need following: 1. start/end timestamp for each test case (most important) 2. immediate report about exceptions (stacktrace) 3. it will be nice to use logging module for output
2
3430
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 method. In this case all works fine: public void TestInitialize() {
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.