As I'm a bit of a fan of Phil Nash's Catch test framework I decided to convert the code in Jeff Langr's book to use Catch. This post describes the changes that were required for the first tests, i.e. chapter 2.
Catch is header only so is much simpler to incorporate than Google Mock/Test so the CMakeLists.txt can be much simpler. For the start of the chapter, this is sufficient:
All we need to do is change the path to the Catch include directory, remove the link path and remove the link libraries.
Similarly, main.cpp is also simpler:
12
#define CATCH_CONFIG_MAIN#include "catch.hpp"
Strictly speaking we don't need main.cpp at all, but it simplifies the test code to keep it.
First 4 examples
The first examples are not very challenging and all that needs to be changed is to #include "catch.hpp" and change the TEST macro for a TEST_CASE with quotes around the identifiers, e.g. example 4:
As you can see, the output provides both the original expression and the reconstructed expression with the values the caused the assertion failure. Much better - Jeff’s explanation would have been much shorter with this!
The fix in example 6 gives this output:
1
Alltestspassed(1assertionin1testcase)
In Catch, example 7 id identical to example 6 so has been removed.
Examples 9
This requires the same type of changes as above, except that the test case name needs to be unique…we'll fix that in example 10.
Example 10,11
We can use Catch SECTIONs to simplify this example…so we don't need the SoundexEncoding class at all, and the test looks like this:
In example 23, Jeff disables a test. We can't do the same thing with a Catch SECTION (yet…) but we can disable an entire test by using the '[hide]' tag, so we temporarily put the test in a TEST_CASE on its own: