Archive for the ‘testing’ Category

Unit testing – with a Catch

Saturday, June 9th, 2012

For fun, I’ve been working on a little iPhone project (a simple password manager – the code is pretty horrible right now, but you can find it on github if you’re curious).

Naturally, I wanted to write some unit tests – and thought it was about time to try out a framework I’ve been meaning to take a look at for a while; it’s called Catch, and written by a friend (and sometime colleague) of mine – Phil Nash. First impressions are very good.

It’s amazingly easy to get up and running; just download it, include a header file – and write a few tests. Here’s a trivial example, with a couple of tests:


#define CATCH_CONFIG_MAIN #include "catch.hpp"
TEST_CASE( "Simple/Number1", "A simple test" )
  int i = 0;
  REQUIRE( i == 1 ); // Naturally, this should fail!
TEST_CASE( "Simple/Number2", "Another test" )
  int i = 0;
  REQUIRE( i == 0 ); // .. and this should pass

And there you go – compile that, and run it – the output looks something like this:

[Started testing]

[Running: Simple/Number1]
test.cpp:7: i == 1 failed for: 0 == 1
[Finished: 'Simple/Number1' 1 test case failed (1 assertion failed)]

[Testing completed. 1 of 2 test cases failed (1 of 2 assertions failed)]


It has some very nice features:

  • It works for both C++ and Objective-C (strictly, the test cases need to be in Objective-C++, but you test Objective-C components with them)
  • The ease of use of the TEST_CASE macro, which hides all the horrid boilerplate that many frameworks seem to insist on.
  • It works on multiple platforms (although so far I’ve only actually run it on OS X).
  • Distributed under the Boost licence, which is about as permissive as they come.
  • It’s a header-only implementation, so no libraries to build and link to.
  • It’s much easier to get yourself started than any other testing framework I’ve used (and I’ve used a few…)

So what’s the Catch? Well, none that I can see – aside from Phil reserves the right to use that particular pun well past it’s sell by date… (Phil has a truly astonishing stockpile of puns, so I’m not sure why he thinks he needs another – but there you go…)

It’s early days for me with Catch, but I’m sure I’ll write more on it some other time. But for now, it gets my recommendation.