views:

1542

answers:

11

I'd like to start using a Test Driven Development system for a private project since I saw my employer using it and realized it was very useful. My employer's project was in C# but mines are in C and C++.

I looked around and saw that several packages exist for both Java and .NET (for example: NCover, NUnit, ...). Unfortunately I found it difficult to find good C++ testing frameworks.

Do you know of any unit testing frameworks that satisfy the following requirements?

  • IMPORTANT: Must provide code coverage statistics, as I'd like to have some idea of how well my tests cover my code-base.
  • Must be free
  • Usable with C++ projects

EDIT: To be clear, I know of many existing unit test frameworks. The code coverage piece is what's most important.

+1  A: 

Well, it doesn't provide code coverage, but Boost's Unit Testing Framework is superb.

rlbond
A: 

You can still use NUnit. Just wrap your Native Classes in C++/ClI managed classes, and write the nunit tests running them. It's a bit of work, but it's worth it for NUnit's testing abilities, and as an added bonus, you'll be able to script your app using IronPython.

Mark P Neyer
Are you saying I'd have to write a wrapper for every class in my application?
Billy ONeal
A: 

I think you may struggle to find a free unit testing framework that'll give you exactly what you require in terms of reporting code coverage.

If you're willing to pay, Bullseye provide a unit testing framework that can report code coverage (it uses instrumentation to achieve this) in a nice interface.

Also, here's a useful article describing a search for C++ unit test coverage.

Alan
That's sure be nice :) Too bad all my stuff is freeware. :( I'd have to save up for 3 months for that app.
Billy ONeal
+10  A: 

Read this

dimba
That article rocks! It helped me choose CxxTest as a framework for placing a testing harness on some ugly legacy code.
Pete
None of the tools mentioned there support coverage :( Otherwise good find +1 :)
Billy ONeal
@idimbe ... the power of two words ..wow.. you know i would put it as a comment becouse it is two words..it would be better if you paste a conclusion(the related info) ..+1 good source
jjj
That's a really good article, but it's REALLY OLD (2004). Since then, the article's author wrote UnitTest++ to address all of the shortcomings his article points out in other suites of that time period, Boost.Test has come a long way, and Google Test (my personal favoite) has been released.
Josh Kelley
Namely that `boost::test` now supports fixtures for entire suites instead of for each test.
Billy ONeal
@Josh Kelley ... people should begin with the GOOD OLD stuff to to be a good start and to understand the new ..!
jjj
@jjj: Are you being sarcastic? Occasionally this is correct advice, but when we're talking about past and current versions of the same software package, that's not true. You spend too much time engineering around problems in the older software that may be fixed in the new software -- such as the lack of good fixtures in older versions of `boost::test`.
Billy ONeal
+5  A: 

I use boost unit test daily and I am quite happy with that library. I remember reading this post about this subject a time ago. It was a really good summary of the different libraries but today it is a bit out to date.

Another option is the google C++ unit test framework, googletest. I have never used but I've read good opinions about it and it is open source

Regarding the coverage I have used the tool included in gcc, gcov. There is even a gui, ggcov.

EDIT: Recently on a team meeting me and other colleagues had a discussion about the limitations of gcov and we wondered if there were better tools out there. Doing some Internet searchings I found a tool called trucov. We haven't got time to evaluate it yet but it looks promising. It is linked to gcc so it works both Linux and Windows. One problem is that you need to use the gcc on windows with might be problem, specially if you just use Visual Studio compiler in your projects. Another issue is that they provide a GUI tool but is still under development.

fco.javier.sanz
The link should be http://code.google.com/p/googletest/
Pete
+1 for Google Test
Billy ONeal
Fixed the link, thanks.
fco.javier.sanz
+2  A: 

My shop uses a variant of covtool for code coverage. You might find it useful.

Fred Larson
Seems to be unix only. Know of anything that works on Windows?
Billy ONeal
A: 

If you are using GCC, then GCOV should work.

If you are not using GCC (e.g., you are using MS or some other commercial C++ compiler), then I don't think you can find free test coverage tools for C++.

Not free, but handles C++ (in a variety of dialects including MS and GNU) as well as a variety of other languages and their dialects (C, C#, Java, COBOL, PHP):

http://www.semanticdesigns.com/Products/TestCoverage

Ira Baxter
:( That's two weeks' pay for me.
Billy ONeal
They pay you $125 per week? $3/hour? I think I'd get a job flipping burgers.
Ira Baxter
...sorry, didn't realize your were just 18, probably only have some part time job...
Ira Baxter
No, I don't work 40 hours/week hehe. Nah, I understand :P
Billy ONeal
And I actually get paid a bit more than that but I subtract off stuff like car insurance which is outside of my control ;)
Billy ONeal
If you are using this just for your own educucational experience, I might be able to provide you with a free copy of one of our tools. Contact me offline (see my bio for email address).
Ira Baxter
+3  A: 

Are you developing in Microsoft Visual Studio 2005 or 2008, and if so, do you have Team System installed? If the answer is "yes", then you already have a code coverage tool at your disposal. It's not "free as in GPL", but if you have Team System you've already paid for it and it's installed on your box.

See this article for how to use vsinstr.exe from the command line to instrument your executable, and vsperfmon.exe to collect the run-time data. You can exercise the code any way you like, including through external automated unit test suites such as CppUnit or via manual execution. Viewing the results is slick: Visual Studio gives you a tree with coverage percents, you can expand the tree down to single functions, and if you double-click a function it highlights the executed lines. The only thing I don't like about it is that libraries such as the STL or CppUnit leave a lot of noisy results you have to scroll past, and I haven't figured out how to selectively suppress results of code that are not under my control.

John Deters
I've only got professional :(
Billy ONeal
+1  A: 

How about: http://www.xcover.org/documentation/0.2.1/

I haven't tested it myself but planning to.

ossandcad
A: 

If you are using GCC's gcov you might want to check out LCOV which provides a nice html coverage report.

Kristian
+1  A: 

I use xCover (with Visual C++ 2005)

dcw