Testing More than One Thing with Catch

In a modern piece of software, you wouldn’t have tests for just one class.  You’d have exhaustive tests for all the functionality in your program.  However, it would be challenging to put all of the tests in one single tests.cpp file.  

With the Catch library, you can have multiple files that each contain a TEST_CASE. Let’s assume that you’re implementing a string class and a vector container class.  It would be possible (and even advisable) to separate your tests into a file per class:

  • stringTest.cpp — Tests related to the string class
  • vectorTest.cpp — Tests related to the vector class

#include "../catch2/catch.hpp"

#include <cstring>
#include "dsvector.h"
#include "dsstring.h"

TEST_CASE("Vector Class", "[vector]"){

   // Set up code

   SECTION("Inserting Properly")
   {
        //some tests
   }

   //some more sections as needed
}

It is likely that your vector class is templated as well.  So, you might even want to have a few different test cases that use different types of type parameters – a primitive, an object, another container (think vector of vectors).

Speak Your Mind

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.