Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kien Nguyen Chi
Kien Nguyen Chi

Posted on

     

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 downloadedcatch.hpp file fromCatch2 Repo.
  • I created a foldertest to maintaincatch.hpp and other testing files.
  • I added testing file with the formattest#.cpp file (# represents for number accordingly)
  • After completing the test, I executed the commandg++ ./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 definedCONFIG_CATCH_MAIN, includecatch.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 modeExit fullscreen mode
  • In theTEST_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 casetest1.cpp, I includedpgprogram.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 modeExit 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 modeExit fullscreen mode
  • Intest2.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 modeExit 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 modeExit 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 modeExit fullscreen mode

Conclusion

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

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.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Programming Student at Seneca College
  • Location
    Toronto, ON, Canada
  • Education
    Seneca College
  • Joined

More fromKien Nguyen Chi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp