Catch2 test framework
In one of our previous posts, we discussed the LRU cache algorithm. How do we go about writing tests for such an algorithm or in general any code we write? There are a multitude of test frameworks to choose from - Google Test, Boost Test, etc., But, I am going to cover the Catch2 framework here as it is very easy to get started with and works really well for most simple use cases. It is also a single header file so very simple to integrate.
Getting Catch2 single header library
As of this writing, the latest Catch2 header is maintained at this branch. And you can directly get the single header file from this link.
Using Catch2 to test LRU Cache
Catch2 usage is quite easy to understand with a simple example. Here is the code needed to test our LRU cache implementation (replicating the original code here for convenience):
How does it work?
- Note that there is no main() function. We instruct Catch2 to provide it by defining
CATCH_CONFIG_MAIN- there is a way to override it for more complex use-cases.
- We have two simple TEST_CASEs. You can think of them as functions with some tags.
- TEST_CASEs can also have SECTIONs. SECTIONs share the code outside their scope
- REQUIRE statements help us to check whether the expected values are returned by our functions
- We can tag test cases and have control to execute a specific test case or even a section
- You can selectively run a specific test case by tag (lrucache) or by name
- There are many more cool features - quite well documented in the Catch2 documentation page