I've been reading a lot about Unit tests and Test Driven developemnt.
Recently, I also read java unit test code.
I however, prefer to develop in Qt. So I googled up "unit testing in c++" and found a host of information about various unit testing frameworks available for C++.
However, I could not find a reliable comparison of the the various frameworks.
So I look to the SO community to guide me through the selection of what may the "best" unit testing framework for c++.
Also, if anybody had specific comments regarding TDD in Qt (especially using Qt-Creator), then they are more than welcome.
Usually use Boost, but if you are using Qt, their QtTestLib might be the better choice.
I would recommend doctest (created by me) - it's the lightest on compile times from all the popular testing frameworks. It is also a direct competitor to Catch which is currently the most used framework - checkout the differences in the FAQ
This seems too be the same question as:
Unit testing in C++ which is actually c++ despite the URL title.
From there, they link to two more SO questions which should help:
Unit testing for C++ code - Tools and methodology
C++ unit testing framework
There is a table comparing all (?) the C++ unit test frameworks available from wikipedia.
There also is an old comparison of C++ unit test frameworks available. I do not think it has not been updated so I mention it as a complement as it's more argumented than the table. It covers, CppUnit, CppUnitLite, Boost.Test, NanoCppUnit, Unit++, CxxTest, especially it does not cover Google C++ framework.
The "xUnit" family of testing frameworks is usually pretty solid (jUnit, NUnit, etc.). I haven't used it myself, but there is a port of jUnit for C++:
http://sourceforge.net/projects/cppunit
Boost is usually a good choice, and it contains a testing framework, the Boost Test Library. I have used it for small test cases and it did what I expected, but I haven't used it extensively like in TTD.
If you want to get off the ground quickly without figuring out how to build a library, there is a single header file include solution, which supports fixtures (setup and teardown), the usual TEST() {} with CHECK_TRUE, etc.
It also has memory leak detection and performance testing capabilities.
https://gitlab.com/cppocl/unit_test_framework
Related
So I found a few older questions asking about BDD frameworks for C++. CppSpec was recommended as a BDD-style framework, but the framework is not nearly as elegant as RSpec or even googletest.
I also saw mentioning of an article detailing Unit Testing C and C++ with Ruby and RSpec which sounded really interesting. However, the article states that there are a lot of limitations with using this method with C++. Has this gotten any better? If not with Ruby, has SWIG become better at interfacing C++ and Python? Could I then attach something like Cucumber?
The last thing that occurred to me was to use googlemock together with googletest (which I'm already using some for unit testing), though it still doesn't seem as elegant or quick as using Ruby or Python BDD frameworks.
I think the key to making BDD/TDD work is that writing tests should be quick and painless. I'm trying to introduce these and other development methods at work and I may need to convince people that writing tests can be short, sweet, and easy.
Update
I just found out about Kross, which might work well because the application uses Qt and targets a Linux environment. Could this potentially be easier/better than SWIG?
Have you taken a look att Igloo?
We don't have nearly as many features as for instance googletest, but we created it with the intention that you shouldn't have to repeat yourself, and we took some inspiration from RSpec and NUnit, and tried to create something pleasant.
Disclaimer: If it's not obvious already, I'm one of the developers behind Igloo.
I'm thinking of designing my own Test library (framework) ->in c++
I am wondering if some of you have already designed there own (and what good advices, documentation they could offer me), decided to not do that (and why), What critics (and argue) you have against differents existing testing frameworks.
I want to no more about testing framework design.
In fact I have some pretty differents things to test :
simple unit test
MVC and signal slot
data, (escpecially for audio and DSP)
performance
compatibility
"So much things ... and so few time "
No really I need to test a lot of different things.
So I checkout how is designed XUnit, and the Addison and W XUnit related book, also the Advanced Unit Testing related article on code project....
And different articles, discuss this with coworkers ...
And at the end, I want to design my own.
Why :
specific needs,
like the do it yourself way (and learn why it's done this way in existing frameworks and that I'm not a genious ... ^^)
Thank you all.
I remember having read some discussions about Cppunit 2 design on the sourceforge wiki. I'd start from here. Also, Noel Llopis explored the C++ unit-testing framework jungle.
But, you're saying you want to re-create another framework and you only have few time left. I'd suggest picking one framework fitting your need for the unit tests, see if it can be used fore your MVC and data testing. Moreover unit testing framework are not designed to run performance tests. I'd recommend following the Unix philosophy here: simple little tools that do one thing and do it well.
Learn at least one existing framwork before you implement your own. My experience is that the framework is not the problem. Learning how to write good unit tests is the hard part.
I have used several framworks through the years including CxxTest, CppUnitLite and UnitTest++. But my recommendation is Google Test together with Google Mock (Google Mock comes with a copy of Google Test bundled).
I found Open source projects with good quality tests but I wanted to ask something a bit different.
I'm having a hard time visualizing how to build production code using TDD practices, particularly for networked database-driven applications where big chunks of functionality are dependent on one or more external systems. The two main strategies I've seen discussed for accomplishing that are decoupling code from the systems in question and using mocks. However, my intuition is that doing either one properly would also be complex and error-prone.
I'd like to take a look at some real-life code that was built using test-driven development practices from the ground up. Such a project would have had to deal with such issues from very early on, and I think looking at the results would be instructive. Any examples out there, whether positive or negative?
Some testing frameworks come to mind ...
JUnit
FitNesse
RSpec
(Though of course this was BDD rather than TDD!)
Both the Ioke and Seph programming languages, as well as their implementations ikj (Ioke interpreter for the JVM), ikc (Ioke interpreter for the CLI) and whatever the Seph implementations is called, and their standard libraries were written 100% test-driven or rather behavior-driven.
Since Autofixture was created to facilitate TDD, I'm sure that the source code for the project itself was developed according.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
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 ?
Here are similar questions that you may want to look at:
Unit testing for C++ code - Tools and methodology
C++ unit testing framework
I recommend you check out Google's unit testing framework in addition to CppUnit.
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.
Check CppUnit, it's the jUnit port to C++.
UnitTest++, legend has it that UnitTest++ was written by the author of this comparison of unit testing frameworks.
Good round up here.
We use Boost.Test, and we are able to do good cross platform continuous integration.
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.
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.
I just started using googletest (https://github.com/google/googletest/). Its simple to integrate and I haven't had any problems with it.
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/
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.
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.
Aeryn is another C++ Testing Framework worth looking at
Have a look at CUnitWin32. It includes an example.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
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?
Applying unit tests to legacy code was the very reason Working Effectively with Legacy Code was written. Michael Feathers is the author - as mentioned in other answers, he was involved in the creation of both CppUnit and CppUnitLite.
Google recently released their own library for unit testing C++ apps, called Google Test.
Project on Google Code
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.
Boost has a Testing library which contains support for unit testing. It might be worth checking out.
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.
I'm using the excellent Boost.Test library in conjunction with a much less known but oh-so-awesome Turtle library : a mock object library based on boost.
As a code example speaks better than words, imagine you would like to test a calculator object which works on a view interface (that is Turtle's introductory example) :
// declares a 'mock_view' class implementing 'view'
MOCK_BASE_CLASS( mock_view, view )
{
// implements the 'display' method from 'view' (taking 1 argument)
MOCK_METHOD( display, 1 )
};
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
{
mock_view v;
calculator c( v );
// expects the 'display' method to be called once with a parameter value equal to 0
MOCK_EXPECT( v, display ).once().with( 0 );
c.add( 0, 0 );
}
See how easy and verbose it is do declare expectation on the mock object ? Obviously, test is failed if expectations are not met.
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.
CxxTest is a light, easy to use and cross platform JUnit/CppUnit/xUnit-like framework for C++.
CppUnit is the way. See link below:
http://cppunit.sourceforge.net/cppunit-wiki
http://en.wikipedia.org/wiki/CppUnit
UnitTest++, small & simple.
I am currently looking for a unit test and mock framework that can be used at our company for a long lived code-base. As you know the list of unit testing frameworks for c++ is long so I applied some filters to reduce it to a hand-full that can be looked in more closely. The first filter criterion was that it must be for free. The second criterion was project activity. I also looked for mocking frameworks because you need one if you want to write unit-tests.
I came up with the following list (approximately) sorted by activity, highest activity at the top:
GoogleTest / GoogleMock: Many contributers and used by Google itself. This will probably be here for some time and receive updates. For my private code-base I will switch to this combination in hopes to jump on the fastest train.
BoostTest + Turtle: Not updated that often, but the testing framework is a part of boost so it should be maintained. Turtle on the other hand is maintained by mainly one guy, but it has resent activity so it is not dead.
I made almost all my testing experience with this combination because we already used the boost library at my previous job and I currently use it for my private code.
CppUTest: Provides testing and mocking. This project has been active from 2008 to 2015 and has quite a lot recent activity. This find was a little suprise because a lot of projects with significantly less activity come up more often when searching on the web (like CppUnit which had its last update in 2013). I have not looked deeper into this so I can't say anything about the details.
Edit (16.12.2015): I recently tried this out and found this framework to be a little clumsy and "C-stylish", especially when using the mock classes. Also it seemed to have a smaller variety of assertions then other frameworks. I think its main strength is that it can be used with pure C projects.
QTest: The test library that ships with the Qt framework. Maintanance should be guaranteed for some time, but I use it rather as a supporting library, because the test-registration is IMO more clumsy then in other frameworks. As far as I understand it, it forces you to have one test-exe per test-fixture. But the test helper functions can be of good use when testing Qt-Gui code. It has no mocks.
Catch: It has recent activity but is mainly developed by one guy. The nice thing about this framework is the alternative fixture approach that lets you write reusable fixture code in the test itself. It also lets you set test names as strings which is nice when you tend to write whole sentences as test names. I whish this style would be ripped of and put into googleTest ;-)
Mock Frameworks
The number of mock frameworks is much smaller then the number of test frameworks but here are the ones that I found to have recent activity.
Hippomock: Active from 2008 unitl now but only with low intensity.
FakeIt: Active from 2013 unitl now but more or less developed by one guy.
Conclusion
If your code-base is in for the long run, choose between between BoostTest + Turtle and GoogleTest + GoogleMock. I think those two will have long term maintenance. If you only have a short lived code-base you could try out Catch which has a nice syntax. Then you would need to additionally choose a mocking framework. If you work with Visual Studio you can download test-runner adaptors for BoostTest and GoogleTest, that will allow you to run the tests with the test runner GUI that is integrated into VS.
See also the answers to the closely related question "choosing a c++ unit testing tool/framework", here
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.
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.)
CppUTest is an excellent, light-weight framework for C and C++ unit-testing.
Michael Feathers of ObjectMentor was instrumental in the development of both CppUnit and CppUnitLite.
He now recommends CppUnitLite
Have a look at CUnitWin32. It's written for MS Visual C. It includes an example.
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.
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.
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.
Check out fructose: http://sourceforge.net/projects/fructose/
It's a very simple framework, containing only header files and thus easy portable.
I'm using MS Test with Typemock Isolator++. Give it a try!