473,748 Members | 9,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use CPPUnit effectively?

How to use CPPUnit effectively?
When I want to use CPPUnit, I always fint it useless and waste my time.
I think that's mainly
because I don't know how to use CPPUnit effectively. So if somebody
here has some experience,
please share it to me.
And also I expect answers to these questions:
1. Should I write test codes for every member function of my class? If
not, which function should be tested?
2. What else should I consider apart from checking parameters and
return value?

Thanks.

Oct 31 '06
59 12630
Ian Collins wrote:
Gianni Mariani wrote:
>Ian Collins wrote:
....
>>Indeed, but I'd advise against writing MT units tests. As you have
pointed out before, that level of testing doesn't help with TDD and is
best left to the acceptance tests.

Unless you're designing something is multi threaded by design. For
example, If the type is performing some kind of asynchronous event
management, it's kind of useless to write a TDD unit test for it that
does not show the MT aspects...
You can test the logic without running in an MT environment.

Same with testing something like a signal handler or interrupt service
routine, you don't have to send an event to test the code. You know the
underlying OS/hardware will deliver the event, your job is to build the
code that processes it.
That's the point. In a limited number of cases, you can't.

Show me what your timer test code looks like ?
Nov 2 '06 #41
Gianni Mariani wrote:
Ian Collins wrote:
>Gianni Mariani wrote:
>>Ian Collins wrote:
....

Indeed, but I'd advise against writing MT units tests. As you have
pointed out before, that level of testing doesn't help with TDD and is
best left to the acceptance tests.
Unless you're designing something is multi threaded by design. For
example, If the type is performing some kind of asynchronous event
management, it's kind of useless to write a TDD unit test for it that
does not show the MT aspects...
You can test the logic without running in an MT environment.

Same with testing something like a signal handler or interrupt service
routine, you don't have to send an event to test the code. You know the
underlying OS/hardware will deliver the event, your job is to build the
code that processes it.


That's the point. In a limited number of cases, you can't.
Can't what? Build the code, or test it?

Code that provides the *lowest level* threading functionality has to be
tested in an MT environment, but a) I'd argue this doesn't constitute
unit testing and b) how often do you write that type of code, once per
project? Once per platform? Once?

If you are developing an event handler, decouple the processing logic
from whatever connects it to the source of the event. Care to provide
an example of where this can't be done?

Show me what your timer test code looks like ?
I don't have any, but I'm sure the OS developer does.

--
Ian Collins.
Nov 2 '06 #42
Ian Collins wrote:
Gianni Mariani wrote:
>Ian Collins wrote:
>>Gianni Mariani wrote:

Ian Collins wrote:
....

Indeed, but I'd advise against writing MT units tests. As you have
pointed out before, that level of testing doesn't help with TDD and is
best left to the acceptance tests.

Unless you're designing something is multi threaded by design. For
example, If the type is performing some kind of asynchronous event
management , it's kind of useless to write a TDD unit test for it that
does not show the MT aspects...

You can test the logic without running in an MT environment.

Same with testing something like a signal handler or interrupt service
routine, you don't have to send an event to test the code. You know the
underlying OS/hardware will deliver the event, your job is to build the
code that processes it.

That's the point. In a limited number of cases, you can't.
Can't what? Build the code, or test it?
Appreciate the implications of the design.
>
Code that provides the *lowest level* threading functionality has to be
tested in an MT environment, but a) I'd argue this doesn't constitute
unit testing and b) how often do you write that type of code, once per
project? Once per platform? Once?
It depends on the asynchronous nature of the problem you're trying to solve.
>
If you are developing an event handler, decouple the processing logic
from whatever connects it to the source of the event. Care to provide
an example of where this can't be done?
Sure, the Austria C++ thread pool design unit test. Maybe the Austria
C++ timer unit test. Both of these tests must show how you deal with
the inherent multi threaded nature of the service you're designing
otherwise you're not really seeing all the design issues.
>
>Show me what your timer test code looks like ?

I don't have any, but I'm sure the OS developer does.
Actually you make my point, if they had a unit test before they coded
they probably would have changed the design. The O.S. timer facilities
are very difficult to use correctly. For example, most OS timer
services come back with a signal. This means you can't do anything that
takes a resource. About the only thing you can do safely is set a flag
or call some other "signal safe" OS calls (like write a byte to a pipe).

The code below shows what the Austria timer unit test "kernel" looks
like. Event(...) gets called back in the "timer service" thread. It
doesn't look like it but it is inherently multi threaded !
class MyTimerClient
: public TimerClient_Bas ic
{

TimerLog & m_log;
int m_client_number ;

public:

MyTimerClient( TimerLog & io_log, int i_client_number )
: m_log( io_log ),
m_client_number ( i_client_number )
{
}

protected:

virtual bool Reschedule(
const TimeStamp & i_current_time,
TimeStamp & o_reschedule_ti me
) = 0;

private:

virtual bool Event(
const TimeStamp & i_current_time,
TimeStamp & o_reschedule_ti me
)
{
m_log.push_back ( m_client_number , ThreadSystemTim e() );
return Reschedule( i_current_time, o_reschedule_ti me );
}

};

Nov 2 '06 #43
Gianni Mariani wrote:
>Same with testing something like a signal handler or interrupt service
routine, you don't have to send an event to test the code. You know the
underlying OS/hardware will deliver the event, your job is to build the
code that processes it.

That's the point. In a limited number of cases, you can't.
CppUnit and its ilk can be used for QA testing, but that's not what we've
been discussing. We are discussing TDD, where we write tests that help force
our lines of code to exist. This helps to refactor our designs as they grow.

If we are not writing a kernel, we don't need to TDD its event systems. We
won't refactor our kernel, so gaps in our test coverage are very low risk.

So you can safely rely on a policy "wait till we have a bug there". (Note
this is the policy that test-free programs already follow - for everything!)
Show me what your timer test code looks like ?
TEST_(TestDialo g, SetTimer)
{
CButton slowButton = m_aDlg.GetDlgIt em(IDC_SLOW_OPE RATION);
slowButton.Send Message(BM_CLIC K);
BOOL thereWasaTimerT oKill = m_aDlg.KillTime r(0);
CPPUNIT_ASSERT( thereWasaTimerT oKill);
}

In Win32 (under WTL), we test that our BM_CLICK handler sets a timer by
killing that timer.

I'm aware that doesn't answer the exact question, but I already had this
answer available!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 2 '06 #44
And if 1000 passes succedes that means that everything is ok? I don't
tink so.

I tink that would be a bad test.
... For example:
example of bad test removed. I can write bad tests too...
Sure it is bad. But how would look a good test that shows that c1 and
c2 may be equal? My point is that such a test doesn't exist.
You can't test every situation all the time, nobody argues that.
Somebody does:
1. You should write tests for each new line (or two) of the code.
Never write
new code without a failing test.
2. If you can't think of the test, how will you think of the line of
code?

I'm just trying to explain that "You can't test every situation all the
time", not that all tests are evil.
I also think that there are more situations that can't be tested than
those that can, but of course I may be wrong here.

Nov 2 '06 #45
Fight them, because they are bad.

And exactly why?
Fix the function so you can
time-slice its algorithm.
That is exactly what OS does. I don't want to rewrite part of OS
because I'm afraid of threads. I want to use code written by OS's
developers and focus on my own application.
we
shouldn't need them, and should resist adding them until we explore all
possible alternatives.
Again, why? Developer's only goal is not to aviod threads, espacially
if they are the best solution.
The question here is simple: Why and how does communication block your GUIs
thread?
- bacause it accesses disk which is slow
- bacause it uses network (not everybody work on windows and even then
not everybody uses networking with messages - Internet Explorer you
mentioned uses now on my computer 9 threads even if it doesn't do
anything currently)
- because I'v just executed sql which will finish in 5 minutes
- because my chess computer really trying to beat me
- because my fractal algorithm for some reason refuses to finish in 2
seconds
- because I'm converting a movie from divx to xvid
....
....
Threading violates encapsulation
thread 1: eatBreakfast()
thread 2: watchTV()

How does it violates encapsulation?
I have eggs on breakfast so:
private:
eatEggs()
But how do I do test it?
I know:
public:
eatEggs()
Fortunatelly in c++ I can make friends with eggs, but now I have test
reference in my code. Good thing is that it doesn't do anything wrong.
So test may break encapsulation (and may not), propelly designed thread
shouldn't.
What is the purpose of test if I write test so it allways passes?

A test here and there that can't fail is mostly harmless.
Obviously. So is scratching behind my ear. It also doesn't do anything
good.
Change 1 global
settings and pray that it is changed with correct proportion?
To continue your digression here, one might ask what we expect to do without
tests when we change 1 global variable
I ment setting in the test. Global variables in a program if they exist
should be changed, possibly in an automated test so there will be no
surprises ater change from const int days_in_current _year from 365 to
366. Or possibly to 543. Its just a number, it should work.

Does really tests that run few hours/days are run on all machines in a
company?

Yes, because developers are running all the tests before integrating their
code. If any test fails, they don't submit. So the same tests must pass on
every machine, many times a day.
Test that last few days should pass on developer machine many times a
day?
What's so special about GUIs?
- it's main purpose is to beeing seen
- it requires user input which may not be predicted

Nov 2 '06 #46

Roland Pibinger wrote:
>
For beginners to C++ Unit Tests the following article may be helpful:
Chuck Allison: The Simplest Automated Unit Test Framework That Could
Possibly Work - http://www.ddj.com/dept/cpp/184401279
Why beginners? I recommend this tool for all that don't want a GUI.

Nov 2 '06 #47
Phlip wrote:
Gianni Mariani wrote:
>>Same with testing something like a signal handler or interrupt service
routine, you don't have to send an event to test the code. You know the
underlying OS/hardware will deliver the event, your job is to build the
code that processes it.
That's the point. In a limited number of cases, you can't.

CppUnit and its ilk can be used for QA testing, but that's not what we've
been discussing. We are discussing TDD, where we write tests that help force
our lines of code to exist. This helps to refactor our designs as they grow.
That is specifically what I talk about.

Hiding the MT nature of a service does not help you design it properly !

If your interface design is inherently MT then show me how to use it in
an MT environment, otherwise I can't evaluate how well it works.
>
If we are not writing a kernel, we don't need to TDD its event systems. We
won't refactor our kernel, so gaps in our test coverage are very low risk.
I disagree. I think this is why I have seen so many abortions of MT
code, because they don't take into account the big 3:

1. resource deadlock (not just mutexes)
2. reliable destruction
3. reliable event management

There are API's I have come across (WININET is one) that you cannot
close properly.

Also, you cannot return a handle from an MT call, it must be made
available to the caller *before* you return.

The point is, of the few classes where asynchronism is critical, you
cannot design it properly unless you take into account the MT nature of
the interface. If you think you can, then show me.

>
So you can safely rely on a policy "wait till we have a bug there". (Note
this is the policy that test-free programs already follow - for everything!)
After I'm done writing the code, I usually write at least one stress
test which usually finds a couple of bugs and occasionally bugs in the
interface itself.
>
>Show me what your timer test code looks like ?

TEST_(TestDialo g, SetTimer)
{
CButton slowButton = m_aDlg.GetDlgIt em(IDC_SLOW_OPE RATION);
slowButton.Send Message(BM_CLIC K);
BOOL thereWasaTimerT oKill = m_aDlg.KillTime r(0);
CPPUNIT_ASSERT( thereWasaTimerT oKill);
}
Not a very useful timer interface, how can I reset the timer ? Can I
safely delete my client and see the timer get deregistered
automatically? Can I set the timer to an absolute time or an interval?

I'd like an interface that is as independent as possible of the O.S.
interfaces since it's a fundamental too.

Compare it to the Austria C++ timer test and I think you'll see what I mean.

>
I'm aware that doesn't answer the exact question, but I already had this
answer available!
The point is, you can't design something using TDD if you don't show how
it's done. If your interface is dealing with MT issues, there really is
little point in not doing an MT unit test for the purposes on TDD.
Nov 3 '06 #48
Gianni Mariani wrote:
>
The point is, you can't design something using TDD if you don't show how
it's done. If your interface is dealing with MT issues, there really is
little point in not doing an MT unit test for the purposes on TDD.
I think we'll have to agree to disagree on this point, I still think you
can't write an MT unit test in a TDD context. Why? Because it's all
but impossible to write a single pass MT test that has a 100%
predictable outcome. There are too many variables, how busy the machine
is, which thread starts first, is there more than one CPU available when
the test runs... So you end up getting unexpected and often
unrepeatable test failures.

Sure you can cure a lot of these inconsistencies by binding everything
to one CPU, but you aren't doing an MT test and may as well mock the
threading layer.

But you can TDD the logic of an MT object and use a separate Monte Carlo
type acceptance stress test for the MT functionality.

One side effect I found doing this was I could easily adapt objects
(like a message queue) from single threaded to MT and event models.
Same logic, different engine.

--
Ian Collins.
Nov 3 '06 #49
Ian Collins wrote:
I think we'll have to agree to disagree on this point, I still think you
can't write an MT unit test in a TDD context. Why?
http://groups.google.com/group/comp....71f34966e6c5e1

Hence

http://www.xprogramming.com/xpmag/ac...ilosophers.htm

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 3 '06 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
3519
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved by pointer in C++, is there such way in Python? Thansk in advance. J.R.
0
1621
by: Roy Smith | last post by:
I'm writing a network application in C++, using CppUnit for unit testing. I'm thinking of forking a subprocess to run tcpdump in some of my unit tests to watch actual packets on the wire as they are transmitted and received. Has anybody done anything like this? Any words of advice (or discouragement)?
2
3832
by: Scott | last post by:
I'm trying to run cppunit on my system under Mac OS X 10.3.3 with Xcode, and I'm getting the following error when I try to run the program: ZeroLink: unknown symbol '__ZN7CppUnit8TestCase3runEPNS_10TestResultE' prog has exited due to signal 6 (SIGABRT). Any idea why I would be seeing this? The program compiles with no problems, it just looks like it can't find a method it needs. I've
9
1971
by: Steven T. Hatton | last post by:
I finally got this thing to build. There's something to be said for using the release of the cvs image sometimes. :-/ I started reading the docs, and this example struck me as a fundamentally bad design for C++. Perhaps it's not bad design in the sense that it will fail, or that it can't be maintained. But there seems to be something fundamentally un-C++ about this. Does anybody else see what I'm talking about here? class...
1
1836
by: To Forum | last post by:
hi, After searching around with google, I have not reach a final answer for my problem with installation with CPPUNIT. 1/ how can I register the dll file in VC7, please tell me in detail 2/ have any one succeed with DSPPlugin. I have found on the page of CPPUNIT a partial answer http://cppunit.sourceforge.net/cgi-bin/moin.cgi/BuildingCppUnit1 but still, I wonder if anyone have a better solution. Thanks TF
0
846
by: skip | last post by:
Manish> It does not work. I had already tried this earlier. Manish> Please suggest some other solutions. Manish> Also, I would like to see the stack from where the exception Manish> started. Manish, You made it extremely difficult for anyone to respond intelligently to your message. It has at least the following problems:
3
3578
by: Belebele | last post by:
Suppose I want to test several implementations of the same protocol (interface with clearly defined semantics) using cppUnit. How to reuse the test that checks the semantics? Take, for example, the simple SetterGetter interface and the even simpler implementations Impl1 and Impl2. class SetterGetter { SetterGetter& operator=(SetterGetter const& ); // Not Implemented
4
4157
by: romcab | last post by:
Hi guys, I'd been searching about CPPUnit and I can't find a very good source about it. At first I thought it was a software that I need to install but found out that what I have is the source code. I want to know if i need to build it and how can I use this one. Hope you can help me guys.
0
1220
by: Jane Prusakova | last post by:
Hello, I would like to test some of the classes for being proper objects, the kind of tests that the Orthodox<MyClassextension of CppUnit library does. However, I can't figure out how to get these tests to run. Can somebody point me to an example, or show how to add these tests to the test runner program? Using CppUnit 1.12 on FreeBSD, gcc version 3.4.2.
0
8991
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
9370
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
9321
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
9247
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
8242
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
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
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.