Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State of the art C++ Unit Testing?

What are the most modern approaches to unit testing for the C++ language? The class of the languages with bigger introspection power (like Python) have unit testing frameworks that are somehow more natural to use. Unit tests can be defined more easily. In comparison, the classical CppUnit (based on JUnit) seems to take very conservative approach. Is there anything newer and better that uses the specific power of C++ (or even C++11) to make the life easier?

I have been using CppUnit framework for a while in rather simplistic way for parts of the project on Windows native C++ (Visual Studio 2005 and 2010). We did not choose the Test Driven Development approach earlier, because there already was a lot of legacy code, and we had found it quite difficult to add tests for it. We had to refactor the application, but adding all the nice tests would be time consuming even in the case.

Recently, we have switched to Visual Studio 2013 (because of the C++11 standard implementation), and we are going to start new, rather long-term project.

Having the previous good (small) experience with unit testing, I would like to try the Test Driven Development approach. As the project is not a tiny one (expected size about the same as the older one, i.e. about 200 k lines of code), I prefer rather more easy (but not less capable) framework.

There is a chance the new project could lead to cross-platform implementation (Windows and Linux). There is a unit testing support in Visual Studio 2013, but I have no experience with it and how would it fit with the cross-platform.

So far, I have found the list of unit testing frameworks for C++. However, one cannot see how they differ in principle. I currently have three candidates (conservative choice):

  • Boost -- the probable candidate; testbed for C++ standards; hence it is likely it is to be widely accepted; probably the biggest user group. It seems to be more advanced than CppUnit.
  • CppUnit -- I know it, but it writing all of the code around is not a pleasure.
  • Visual Studio 2013 built-in -- new to me, somehow can generate the skeletons probably.

Anyway, it seems that all of the three use similar approach. Possibly the VS2013 supports generating the code, but it does not mean it leads to anything simpler.

Is there any radically new approach?

like image 677
pepr Avatar asked Dec 16 '13 08:12

pepr


People also ask

What is C unit testing?

A Unit Testing Framework for C CUnit is a lightweight system for writing, administering, and running unit tests in C. It provides C programmers a basic testing functionality with a flexible variety of user interfaces. CUnit is built as a static library which is linked with the user's testing code.

Which is not a part of unit testing?

A test is not a unit-test if: it communicates with a database. it cannot run in parallel with other tests. uses the "environment" like registry or file system.

When did unit testing become popular?

Joe Eames, a front-end developer and Pluralsight instructor who has taught several courses on unit testing, said that, although unit testing had been possible earlier, it really became popular in the late '90s and early 2000s.

What is unit testing with example?

Unit testing is testing the smallest testable unit of an application. It is done during the coding phase by the developers. To perform unit testing, a developer writes a piece of code (unit tests) to verify the code to be tested (unit) is correct.


1 Answers

The only test framework worth considering: Catch

For an introduction to the lib, see also here and here

It's easy to use (a header-only lib consisting of just one header), portable, and has by far the simplest, cleanest syntax of any C++ unit testing framework.

And unlike other libraries, you don't need to remember two dozen different macros f or different types of assertions.

You just use REQUIRE:

int one = 1; REQUIRE( one == 2 ); 

which, through some clever operator overloading, will show both the original expression and the expanded argument values in the output:

test.cc(7): FAILED:   REQUIRE( one == 2 ) with expansion:   1 == 43 

Compared to this, every other framework is a chore to use IMO.

I used to use Boost.Test before I found this, but that was a lot more cumbersome to set up and use. We use CppUnit at work, and that seems to be designed to be as fragile and painful as possible.

I've looked briefly at the VS2013 test framework, but haven't tried it, and it looks tolerable, but very much like it's emulating "the old guard". It doesn't really try to be cleaner, easier or better than CppUnit, Boost.Test and all the other ones that came before Catch. So I'd say don't bother with it. Tests should be easy to write (and to understand), and Catch is lightyears ahead of every other framework I've seen on that front.

like image 109
jalf Avatar answered Sep 29 '22 20:09

jalf