views:

4412

answers:

19

I'm working on a large c++ system that is has been in development for a few years now. As part of an effort to improve the quality of the existing code we engaged on a large long-term refactoring project.

Do you know a good tool that can help me write unit tests in C++? Maybe something similar to Junit or Nunit?

Can anyone give some good advice on the methodology of writing unit tests for modules that were written without unit testing in mind?

+6  A: 

CppUnit is the way. See link below:

http://cppunit.sourceforge.net/cppunit-wiki

http://en.wikipedia.org/wiki/CppUnit

smink
+1  A: 

Michael Feathers of ObjectMentor was instrumental in the development of both CppUnit and CppUnitLite.

He now recommends CppUnitLite

Seb Rose
+3  A: 

I've tried CPPunit and it's not very user friendly.

The only alternative I know is using C++.NET to wrap your C++ classes and writing unit tests with one of .NET unit testing frameworks (NUnit, MBUnit etc.)

Dror Helper
+3  A: 

UnitTest++, small & simple.

yrp
+22  A: 

Google recently released their own library for unit testing C++ apps.

Google [email protected]

agnul
is it possible to use this with VC++
yesraaj
@yesraaj yes, it's possible.
Elazar Leibovich
Seems pretty ok, especially the way they have to add description to each assertion. On the down side, I personally prefer having a Unit Test class instead of macros that really don't look like classes.
Wernight
another nice point is the mocking possibilities: http://code.google.com/p/googlemock/
Philipp
+7  A: 

CxxTest is a light, easy to use and cross platform JUnit/CppUnit/xUnit-like framework for C++.

David Sykes
+12  A: 

Boost has a Testing library which contains support for unit testing. It might be worth checking out.

Jonas
I can recommend this excellent toolkit.
Rob
Yes, boost is the way to go. No overhead, just test and go!I was actually working on my own framework in despair when boost came to my rescue. Thank you boost (for everything!)
daramarak
You may check out an article I wrote introduction Boost Unit Testing http://www.beroux.com/english/articles/boost_unit_testing/
Wernight
+34  A: 
Joe Schneider
Added a thumbnail - voted up. The book helps more than any tool.
Gishu
I think CPPUnit could make it simpler to write tests. We use CPPUnit, but I am not satisfied. I need to update two files for each test, and in my opinion, a test should be as simple to write as:'TEST("testname") {ASSERT(1==1);}'The book on the other hand is a must for everyone, not only those who work with legacy code, but also for those who create it ;)
daramarak
I read the book. Very good read
Sakin
Since when is c++ legacy?!
Nils
IT's not that C++ is legacy - if I recall correctly, that book defines a legacy project as one for which there are none, or very few unit tests.Such projects do tend to be /hard/ to write unit tests in, because test driven development has never influenced the code base such that it is trivial to write them.
Arafangion
@Nils: As one of the Amazon reviewers of the book mentions, "legacy code is code without unit tests," which is exactly what this question is about.
David Johnstone
+7  A: 

Noel Llopis of Games From Within is the author of Exploring the C++ Unit Testing Framework Jungle, a comprehensive (but now dated) evaluation of the various C++ Unit Testing frameworks, as well as a book on game programming. He used CppUnitLite for quite a while, fixing various things, but eventually joined forces with another unit test library author, and produced UnitTest++. We use UnitTest++ here, and I like it a lot, so far. It has (to me) the exact right balance of power with a small footprint. I've used homegrown solutions, CxxTest (which requires Perl), and boost::test. When I implemented unit testing here at my current job it pretty much came down to UnitTest++ vs boost::test. I really like most boost libraries I have used, but IMHO, boost::test is a little too heavy-handed. I especially did not like that it requires you (AFAIK) to implement the main program of the test harness using a boost::test macro. I know that it is not "pure" TDD, but sometimes we need a way to run tests from withing a GUI application, for example when a special test flag is passed in on the command line, and boost::test cannot support this type of scenario. UnitTest++ was the simplest test framework to set up and use that I have encountered in my (limited) experience.

Brian Stewart
A: 

You might also find the Aeryn testing framework worth a look

David Sykes
A: 

Check out fructose: http://sourceforge.net/projects/fructose/

It's a very simple framework, containing only header files and thus easy portable.

+3  A: 

See also the answers to the closely related question "choosing a c++ unit testing tool/framework", here

TonJ
+14  A: 

Check out an excellent comparison between several available suites. The author of that article later developed UnitTest++.

What I particularly like about it (apart from the fact that it handles exceptions etc. well) is that there is a very limited amount of 'administration' around the test cases and test fixtures definition.

andreas buykx
Isnt' that our fundamental fallacy? He's got good insight into available projects - but instead of improving them, he starts his own.
peterchen
+2  A: 

There also is TUT, Template-Unit-Test, a template-based framework. It's syntax is awkward (some called it template-abusing), but its main advantage is that is it all contained in a single header file.

You'll find an example of unit-test written with TUT here.

philippe
+1  A: 

Have a look at CUnitWin32. It's written for MS Visual C. It includes an example.

Dushara
A: 

Have a look at cfix (http://www.cfix-testing.org), it's specialized for Windows C/C++ development and supports both user mode and kernel mode unit testing.

Johannes Passing
A: 

If you are on Visual Studio 2008 SP1, I would highly recommend using MSTest for writing the unit tests. I then use Google mock for writing the mocks. The integration with the IDE is ideal and allows and doesn't carry the overhead of CPPunit in terms of editing three places for the addition of one test.

Jared
A: 

Kindly help me in using UNITTest++. I have downloaded it from the net. It consists UnitTest++\src which have some cpp files. Plz help me _ how to test a code by using UnitTest++.

Vaibhav Bhardwaj
Sounds like a question, not an answer...
daramarak
A: 

I think VisualAssert is doing a great job in VS integration. It lets you run and debug the tests from VS and you don't need to create an executable in order to run the tests.

Ohad Horesh