Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ unit testing framework [closed]

I use the Boost Test framework for my C++ code but there are two problems with it that are probably common to all C++ test frameworks:

  • There is no way to create automatic test stubs (by extracting public functions from selected classes for example).
  • You cannot run a single test - you have to run the entire 'suite' of tests (unless you create lots of different test projects I guess).

Does anyone know of a better testing framework or am I forever to be jealous of the test tools available to Java/.NET developers?

like image 390
Rob Avatar asked Sep 17 '08 21:09

Rob


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.

Does C have unit testing?

One unit testing framework in C is Check; a list of unit testing frameworks in C can be found here and is reproduced below. Depending on how many standard library functions your runtime has, you may or not be able to use one of those.

What are the frameworks to write unit test cases in C#?

The three major C# Unit testing frameworks are MSTest, NUnit, and xUnit.Net.

Does Google test support C?

Google test, or gtest is an open source framework for unit testing C\C++ projects.


2 Answers

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 doesn't do generation of stubs - but that's a fairly specialised area. I think Isolator++ is the first tool to truly pull that off. Note that Mocking/ stubbing frameworks are usually independent of unit testing frameworks. CATCH works particularly well with mock objects as test state is not passed around by context.

It also has Objective-C bindings.

[update]

Just happened back across this answer of mine from a few years ago. Thanks for all the great comments! Obviously Catch has developed on a lot in that time. It now has support for BDD style testing (given/ when/ then), tags, now in a single header, and loads of internal improvements and refinements (e.g. richer command line, clear and expressive output etc). A more up-to-date blog post is here.

like image 183
philsquared Avatar answered Oct 08 '22 12:10

philsquared


Take a look at the Google C++ Testing Framework.

It's used by Google for all of their in-house C++ projects, so it must be pretty good.

http://googletesting.blogspot.com/2008/07/announcing-new-google-c-testing.html

http://code.google.com/p/googletest

like image 38
J Miller Avatar answered Oct 08 '22 13:10

J Miller