tags:

views:

776

answers:

14

Been hitting my head on the wall before as I don't make any test classes while using c/c++ (but instead have a lot of print methods).

What is the most used method to perform testing in the c/c++ code? Java's JUnit has only left me with good memories while debugging things.

I know that using asserts in code with a defined debug header should work, but aren't there any better ways?

And since I have the intention to make test classes in the future, any useful tips are welcome.

+8  A: 

We use boost.Test. There is also cppunit.

chrish
+6  A: 

We use Google Test and it's companion Google Mock. Works wonderfully and supports JUnit style XML output for easy integration with CruiseControl, etc. It's also fully cross platform, and from my research a few months ago, GMock was the ONLY fully cross platform object mocking framework for C++.

Grant Limberg
+1. UnitTest++ is good, but Google Test is amazing.
Josh Kelley
+5  A: 

You can check these out:

http://gamesfromwithin.com/?p=29

http://www.opensourcetesting.org/unit_c.php

http://msdn.microsoft.com/en-us/magazine/cc136757.aspx

bbqchickenrobot
First link to gamesfromwithin has all I needed to know about different frameworks (at least for now). Thank you.
Milan
+3  A: 

UnitTest++ is worth a look. It lacks features compared to some of the other frameworks mentioned here, but the simplicity is nice -- especially if you're just getting started.

I believe it was developed by Noel Llopis after writing the article mentioned in http://gamesfromwithin.com/?p=29. It is pretty easy to port to a multitude of different compilers/platforms -- useful if need to target something other than a PC, be it a game console, embedded device, or otherwise.

The mailinglist has been quiet for a while now, but every so often there are questions, patches, and/or a new release, and people who ask questions are usually answered quickly.

leander
Seconded. I've tried a handful of unit test frameworks for C++, and UnitTest++ is easily the best in my opinion.You are correct that it was developed by Noel Llopis. See http://gamesfromwithin.com/?p=51.
Geerad
+1 UnitTest++ is excellent.
markh44
+1  A: 
Brian
Well, yes. But a good unit testing framework really does a lot more than that.
Johannes Passing
Like couple all of your object files together and force you into pullling in yet another library?
Brian
A: 

Some people like Aeryn

David Sykes
A: 

if you are using windows, take a look at: http://stackoverflow.com/questions/243673/limitations-of-using-c-cli-with-nunit and write your tests in http://en.wikipedia.org/wiki/Managed_Extensions_for_C%2B%2B.

also: http://golios.blogspot.com/2008/12/using-nunit-with-c-part-2.html, http://golios.blogspot.com/2008/11/using-nunit-with-native-c.html

this will work just like junit except for the fact that nunit does not create a new instance of the test case class before running each test.

Ray Tayek
A: 

if it's for Windows platform you can try WinUnit from MS from here. Simplified Unit Testing for Native C++ Application

+1  A: 

Shameless plug: If you target Windows and are using Visual Studio, check out cfix and cfix studio.

It is also compatible to WinUnit.

Johannes Passing
A: 

For Symbian there is SymbianOSUnit: http://www.symbianosunit.co.uk/

Riussi
+1  A: 

Wikipedia has a long list of unit testing frameworks for C++ and another list for C.

David Johnstone
+1  A: 

Hi,

I'm working with Qt under Windows and Linux, wich provides an own integrated (but compared to other separate solutions a little bit limited) testing framework.

It's very easy to use and fast to learn - see QTestLib

ciao, Chris

3DH
+1 , Qt is also great for many other things
MadH
+1  A: 

newest version of Visual Studio has something:

http://msdn.microsoft.com/en-us/library/ms243147(VS.80).aspx

MadH
+1  A: 

Why not use CppUnit? It was created as a port of JUnit.

nathan