tags:

views:

93

answers:

5

Alright. I have done screen scrapping program in c++. Now how do i unit test a .cpp? where do i get started?

+1  A: 

CppUnit is the C++ equivalent of JUnit for unit testing.

Hippo
But it is quite awkward. The original author went on to create a new system which uses macros to eliminate the need to define so much plumbing. This approach is being copied and used in the more recent C++ unit test frameworks.
Jon Reid
yes im using cppUnit now
svenus
+3  A: 

There are several Unit testing frameworks which can help you test your code.

Check out: Google Test (Google C++ Testing Framework) which can be found at http://code.google.com/p/googletest/

and Google Mock (Google C++ Mocking Framework) at http://code.google.com/p/googlemock/ which will help you check your application's flow by creating mock objects for your classes.

(you should read about Mock objects at http://en.wikipedia.org/wiki/Mock_object)

If the GoogleTest framework doesn't fit your needs, You have another great alternative called CxxTest ( http://cxxtest.tigris.org/ ).

Good luck!

Tal.

Tal
I've heard UnitTest++ is also very good: http://unittest-cpp.sourceforge.net/
Mark Simpson
thanks Tal! i am using CPPUNIT as its already in my netbeans.
svenus
A: 

Essentially, what you need to do is take the module/class you want to test and isolate it from the rest of the system, simulate (fake) all of its interactions with any external classes/API's, and verify that it did what it's supposed to do.

Typically you do this simulation using "mock objects". You contrive behavior of the mock objects to exercise different operating conditions for your System (module/class) Under Test (SUT). You could use an existing mock object framework, or you could simply "roll your own" mock objects, by creating objects or functions that implement all of the interfaces your SUT uses.

You'll have to figure out how you can inject your mock objects into your SUT. For example if your SUT had a member variable object instance, perhaps you could make a subclass of the SUT and add a method "SetMemberObj(aMockInstance)" to install the mock object. If your SUT uses global functions, perhaps you can create functions with identical signatures, and NOT link your test app with the .lib that contains the real functions, so that your SUT will call into the fake ones instead.

You'll also have to decide how you can verify the SUT's behavior; in a simple case maybe you could check the return code of a method, whereas in a more complicated situation you'll want to query your mock objects to see how they were invoked by the SUT.

You may also be challenged to figure out how to actually invoke your SUT. What I typically do is make a console .exe that instantiates and drives the SUT in various ways, indicating any errors via stdout and/or return codes. A unit testing framework can be very helpful for this, but isn't entirely necessary.

Thanks i needed that!
svenus
A: 

Here are instructional videos demonstrating test-driven development in C++: http://www.vimeo.com/album/254486

Jon Reid
A: 

There are a lot of C++ unit testing frameworks available so it can be rather daunting to choose one. Here is a very good series of blog posts that evaluate several frameworks and contains lots of examples of C++ unit testing in action.

Dave Kirby
Actually not a huge fan of this comparison simply because it's too old. Several of his gripes with existing frameworks have been resolved in the intervening years (Such as boost::test's former lack of suites).
Billy ONeal