Catch2 - Testing Framework

kiennguyenchi

Kien Nguyen Chi

Posted on November 12, 2021

Catch2 - Testing Framework

Introduction

This week, I work on my Static Site Generator (SSG) - Potato Generator. I implemented the Catch2 - Testing Framework to my project.

There are many testing tools for C++, I could say MSTest, Google Test, built-in test in Visual Studio,... It may take you times to set up the tools. In contrast, I pick a Catch2 framework for testing because it is easy to set up and easy to use.

Process

  • I downloaded catch.hpp file from Catch2 Repo.
  • I created a folder test to maintain catch.hpp and other testing files.
  • I added testing file with the format test#.cpp file (# represents for number accordingly)
  • After completing the test, I executed the command g++ ./test/test#.cpp -o test# --std=c++17 in the CMD and then opened the Output window to see the success or failure of the test.

Testing file structure

  • First of all, every testing file has the following structure. I defined CONFIG_CATCH_MAIN, include catch.hpp file at the beginning of each test file and created each test case with the following format.
#define CONFIG_CATCH_MAIN
#include "catch.hpp"

TEST_CASE("This is the name of the test case")
{
    //code
}
Enter fullscreen mode Exit fullscreen mode
  • In the TEST_CASE, I would use REQUIRE function to ensure the outputs of my testing functions are correctly returned.
  • If you want to test any functions in any specific file, you need to include it in the testing file. In my first test case test1.cpp, I included pgprogram.cpp file to test some functions to validate the command line arguments.
  • I checked if there is no arguments provided. Does the function output the correct messages?
TEST_CASE("No Arguments provided")
{
    char* list[1] = {"./pgprogram"};
    REQUIRE(checkArguments(1, list) == "Failed arguments provided");
}
Enter fullscreen mode Exit fullscreen mode
  • I checked if true argument is provided. Does the function output correct messages? There are tons of tests relating to arguments version, help, input, output, language,... But I gave 1 example here.
TEST_CASE("Arguments Check")
{
    char* list[2] = {"./pgprogram", "-v"};
    REQUIRE(checkArguments(2, list) == "Potato Generator - Version 0.1");
}
Enter fullscreen mode Exit fullscreen mode
  • In test2.cpp, I included "HTMLFile.h" and I implemented some tests to check for the HTML File implementations.
  • Firstly, I checked if the program prints nothing if no files are provided.
TEST_CASE("Check empty files")
{
    HTMLFile file;
    REQUIRE(file.getTitle() == "");
    REQUIRE(file.getURL() != "");
}
Enter fullscreen mode Exit fullscreen mode
  • Secondly, I checked if correct file is input and its URL and title are implemented correctly.
TEST_CASE("Check existing files")
{
    HTMLFile file;
    file.openFile("../Sherlock-Holmes-Selected-Stories/Silver Blaze.txt", "fr");
    REQUIRE(file.getTitle() == "Silver Blaze");
    REQUIRE(file.getURL() != "Silver Blaze.html");
}
Enter fullscreen mode Exit fullscreen mode
  • Lastly, of course, I need to check if incorrect file is input, does the program process anything? In this case, nothing is implemented.
TEST_CASE("Check non-existing files")
{
    HTMLFile file;
    file.openFile("../Sherlock-Holmes-Selected-Stories/notafile.txt", "fr");
    REQUIRE(file.getTitle() == "");
    REQUIRE(file.getURL() != "");
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

You can take a closer look at all the testing files that I created so far and folder structure in the commit.

Overall, the testing tool is really helpful to test our code and it is worth to implement. But also it is overwhelming to brainstorm all the possibilities that could happen to put into the test.

💖 💪 🙅 🚩
kiennguyenchi
Kien Nguyen Chi

Posted on November 12, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related