2

edit: This question might be vote-closed because "not a real programming question". To which I can only retort "nyah!" Now I did find this question which is directly related and should help.

I'm methodically upgrading my source code to get with the C++11 times, and one of the pieces that a lot of my code interacts with is UnitTest++.

I dedicate the latter half of every one of my implementation cpp files to unit tests, so they include many

TEST(testname) {
  // test code
}

declarations.

Now, UnitTest++ is about 8 years old and it still compiles great, so I have no urgent need to replace it. However I have found that it is probably no longer being maintained (though its existing features certainly seem solid enough, this is a bad sign) as the website and sourceforge are down.

So even though my code works fine now, it may benefit me now to switch to a better system earlier rather than later, because it will reduce translation burden in the future.

I looked around a bit and there seem to be a few options available to me. Particularly interesting is libunittest and others like CATCH which is header-only.

My question is for folks who have maybe had experience with UnitTest++ in the past and other unit testing systems, what has worked well for you and if you have any recommendations. I am looking for something that is extremely portable and which has zero external dependencies beyond a C++98/03 or C++11 compiler (gcc, clang, msvc) and the standard libraries, and where being header-only is a plus but not necessary.

So I guess my preferences do tend to narrow down the options quite a bit. Even with UnitTest++ I enjoy its portability and self-containedness, but I have had to write a good ~100 or so lines worth of code to extend it to be flexible for me in two ways:

  • allow me to specify specific tests to run (whether it's choosing the tests by name, or by source file in which they're implemented, or the test suite name)
  • customize reporting behavior for tests such as showing me timing data per test and summarizing totals at the end -- i'm actually somewhat unsatisfied with the capabilities and behavior of this, for example I do not know how to configure UnitTest++ to apply my verbose test reporter to run for tests that are not placed inside a test suite.

A perfect answer would be something like this from none other than one of the guys behind UnitTest++ itself! But that article is 10 years old!!

Community
  • 1
  • 1
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • If it aint broke - why fix it? – Ed Heal Dec 30 '14 at 08:28
  • 1
    @EdHeal Because I am an insufferable perfectionist when it comes to this stuff – Steven Lu Dec 30 '14 at 08:31
  • @stevenLib - might end up breaking the unit tests by doing this! – Ed Heal Dec 30 '14 at 08:32
  • @EdHeal Yes that is a bit of a concern! I don't want to make *too* much extra work for myself with such an overhaul. Which is why I hope to find some folks who might have had a little experience with UnitTest++ who are also familiar with other options. It's likely these individuals can save me a lot of trouble. – Steven Lu Dec 30 '14 at 08:33
  • 1
    [UnitTest++](https://github.com/unittest-cpp/unittest-cpp) is still actively developed and just before you posted this question there were some updates in [November of 2014](https://github.com/unittest-cpp/unittest-cpp/graphs/contributors?from=2014-08-29&to=2015-01-13&type=c). In addition Microsoft uses UnitTest++ to test the [GSL](https://github.com/Microsoft/GSL/tree/master/tests) and [Casablanca](https://github.com/Microsoft/cpprestsdk/tree/master/Release/tests/common) (though they have embedded an old version it is on our todo list to replace with the latest). Hope you have stuck with us! – graham.reeds May 31 '16 at 09:23

2 Answers2

2

You may find Google Test to be of use. It supports everything UnitTest++ does and more, and it is still maintained (and regularly used within Google). Additionally, it has a corresponding mocking library, Google Mock for creating mocks that is pretty useful for creating mock implementations.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • Hmm I guess I am guilty of making assumptions about google test. Actually looking at the source file list it seems to not be as "bloated" as I expected. Thanks. I'll look into it further – Steven Lu Dec 30 '14 at 08:29
  • No problem. Hope it works out for you. (And, if not, I'm curious to hear what you ultimately pick). – Michael Aaron Safyan Dec 30 '14 at 08:30
  • I am rather curious about what specific features google test supports. For example in comparison to libunittest. – Steven Lu Dec 30 '14 at 08:35
  • Hmm, now all 4 options (stick with UT++, google test, libunittest, Catch) seem to have something good going for it (1. lazy option, probably safest 2. momentum of the almighty Google 3. parallel test running, and maybe even more 4. header-only, hierarchical, seems more "innovative") – Steven Lu Dec 30 '14 at 09:04
0

I have two more:

Both are actively developed.


CppUTest

that is extremely portable [...]
[...] beyond a C++98/03 or C++11 compiler (gcc, clang, msvc) and the standard libraries

CppUTest can be used with various compilers and platforms - it's used for embedded systems too. In addition at has a build in C interface.

and which has zero external dependencies

Not much to say: download / checkout and build, done.

and where being header-only is a plus but not necessary.

Unfortunately this is not possible with CppUtest, it requires it's testlibrary for linking.

allow me to specify specific tests to run

Possible and also explained in the manual (not very difficult). See here.

customize reporting behavior for tests such as showing me timing data per test and summarizing totals at the end

This is done per default in verbose mode. It's also possible to export JUnit Xml files with the result. In addition you can customize the output format for all steps and kinds of output.


Example code:

TEST_GROUP(FirstTestGroup)
{
     /*
      * Test group = test suite
      * You can implement tear down / setup here too
      */
};

TEST(FirstTestGroup, FirstTest)
{
   FAIL("Fail me!");
}

TEST(FirstTestGroup, SecondTest)
{
   int value = 4;
   const int expected = 4;

   CHECK_EQUAL(expected, value);
}

Finaly it's not difficult to use: check the Manual.

Btw. it contains mocking (CppUMock) and an integrated memory leak detector.


Documentation:

ollo
  • 24,797
  • 14
  • 106
  • 155
  • Looks pretty decent. Thanks! (I won't test this now though. My decision was to stick with UnitTest++. It's still very solid. CppUTest seems like it's in active development, which means it's not necessarily stable.) – Steven Lu Jan 14 '15 at 22:42
  • Actually not, it means there are people who care about the project and will further improve it. Release version (current: 3.6) are safe and stable to use. Btw. it's a framework which is widely used (eg. in TDD). Books like *Modern C++ Programming with Test-Driven Development* (Jeff Langr) or *Test Driven Development for Embedded C* (James W. Grenning) use and recommend them too. – ollo Jan 15 '15 at 16:08