22

How do I get started doing unit testing in C++ ?

I have used Junit when coding in Java and found it very useful. Is there something similar in C++ ? What do you recommend ?

duli
  • 1,621
  • 3
  • 16
  • 25
  • 6
    @Kim: Yes, and this greate new service led me here, thanks to this question. SO is not just questions and answers, it's also having the community's opinion and feedback about the answers. – ereOn Mar 28 '11 at 08:45

13 Answers13

11

Here are similar questions that you may want to look at:

I recommend you check out Google's unit testing framework in addition to CppUnit.

Community
  • 1
  • 1
David Crow
  • 16,077
  • 8
  • 42
  • 34
8

I recently wrote a 5-part series of blog posts covering unit testing in C++ with boost. It gives step by step instructions for how to do this in Visual Studio.

  • Part 1: how to start making unit tests in C++ with Boost.Test.

  • Part 2: the assertion framework provided by Boost.Test in the context of a simple programming exercise that demonstrates test-driven development.

  • Part 3: continuing with test-driven development to complete the functionality of the PrimeFactors::Generate method.

  • Part 4: test-driven UI development.

  • Part 5: the facilities in Boost.Test for sharing common setup and teardown actions between tests and organizing tests into suites.

legalize
  • 2,214
  • 18
  • 25
7

Check CppUnit, it's the jUnit port to C++.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
7

UnitTest++, legend has it that UnitTest++ was written by the author of this comparison of unit testing frameworks.

oz10
  • 153,307
  • 27
  • 93
  • 128
  • +1, I use this framework and can recommend it! It's more than just a legend. The 1.0 release is here: http://gamesfromwithin.com/unittest-v10-released (on the same blog as the above link...) – Kleist Nov 11 '11 at 11:50
6

Good round up here.

We use Boost.Test, and we are able to do good cross platform continuous integration.

csexton
  • 24,061
  • 15
  • 54
  • 57
4

Take a look at this page: http://gamesfromwithin.com/?p=29

It is the best comparison of the C++ frameworks. I personally prefer Boost.Test.

Kazade
  • 1,297
  • 2
  • 15
  • 25
3

I haven't been happy with any C++ unit testing framework. Since C++ doesn't have reflection, it's hard to write convenient unit testing tools. CxxTest is about as good as I've found. I've used it on some projects, but usually I just write my own tests either without a framework or using a crude framework I wrote myself.

John D. Cook
  • 29,517
  • 10
  • 67
  • 94
3

I just started using googletest (https://github.com/google/googletest/). Its simple to integrate and I haven't had any problems with it.

jxramos
  • 7,356
  • 6
  • 57
  • 105
Jeff M
  • 700
  • 3
  • 10
2

My personal favorite is TUT. The two main reasons are that 1) it doesn't force Java-isms on you but takes advantage of what C++ is, and 2) you have control over it, writing the executable (I have a template I used), the reporting etc (provides a stream based version by default).

To me it very much follows the philosophy of KISS, 2 headers, no macros, no parsers, just plain old C++ code with a tiny bit of skeleton code.

http://tut-framework.sourceforge.net/

Ed.
  • 449
  • 2
  • 6
  • 9
2

I've just pushed my own framework, CATCH, out there. It's still under development but I believe it already surpasses most other frameworks. Different people have different criteria but I've tried to cover most ground without too many trade-offs. Take a look at my linked blog entry for a taster. My top five features are:

  • Header only
  • Auto registration of function and method based tests
  • Decomposes standard C++ expressions into LHS and RHS (so you don't need a whole family of assert macros).
  • Support for nested sections within a function based fixture
  • Name tests using natural language - function/ method names are generated

It also has Objective-C bindings.

philsquared
  • 22,403
  • 12
  • 69
  • 98
1

Without knowing which platform/compiler you are targetting, I can only make a general recommendation. I've used this (CppTest) one quite successfully in the past. There's a simple framework called UnitTest++ that looks interesting.

Pete OHanlon
  • 9,086
  • 2
  • 29
  • 28
1

Aeryn is another C++ Testing Framework worth looking at

David Sykes
  • 48,469
  • 17
  • 71
  • 80
1

Have a look at CUnitWin32. It includes an example.

Dushara
  • 616
  • 1
  • 5
  • 25