Unit testing in C++ [closed]
Asked Answered
V

13

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 ?

Valoniah answered 22/12, 2008 at 20:32 Comment(1)
@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.Jaymie
G
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.

Galloon answered 22/12, 2008 at 20:40 Comment(0)
U
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.

Uneven answered 19/7, 2009 at 16:49 Comment(0)
A
7

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

Ariel answered 22/12, 2008 at 20:35 Comment(0)
D
7

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

Day answered 23/12, 2008 at 3:15 Comment(1)
+1, I use this framework and can recommend it! It's more than just a legend. The 1.0 release is here: gamesfromwithin.com/unittest-v10-released (on the same blog as the above link...)Basketwork
T
6

Good round up here.

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

Tacit answered 22/12, 2008 at 20:40 Comment(0)
U
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.

Urmia answered 22/12, 2008 at 20:40 Comment(0)
D
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.

Duplet answered 23/12, 2008 at 3:19 Comment(1)
You might want to check out CATCH (link from my answer)Gilding
A
3

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

Appointive answered 29/12, 2008 at 7:22 Comment(0)
G
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/

Grantham answered 22/12, 2008 at 20:42 Comment(0)
G
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.

Gilding answered 28/12, 2010 at 3:7 Comment(0)
T
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.

Tundra answered 22/12, 2008 at 20:38 Comment(0)
R
1

Aeryn is another C++ Testing Framework worth looking at

Roorback answered 23/12, 2008 at 8:26 Comment(0)
F
1

Have a look at CUnitWin32. It includes an example.

Flashing answered 12/2, 2009 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.