Develop C++ unit testing with ... Note
GitLab

Develop C++ unit testing with Catch2, JUnit, and GitLab CI

Continuous integration (CI) and automated testing enhance software development workflows by facilitating early bug detection, improving code quality, and streamlining development processes. This tutorial guides you through setting up unit testing on a C++ project with Catch2 and GitLab CI for continuous integration, utilizing GitLab Duo's AI-powered features. The reference project used is an air quality monitoring application that fetches data from the OpenWeatherMap API based on U.S. zip codes. Prerequisites include CMake, a modern C++ compiler, and an API key from OpenWeatherMap. To set up the application for testing, fork the reference project and clone it to your local environment, generate an API key from OpenWeatherMap and export it into the environment, compile and build the project code, and run the application. To install Catch2, create an externals folder and add Catch2 as a submodule, update CMakeLists.txt to include Catch2's directory, and create a tests.cpp file. For structuring the project for testing, separate the application logic into separate files, including main.cpp, includes/functions.cpp, and includes/functions.h. Write tests in Catch2 using macros and assertions, defining a test case container with TEST_CASE and verifying conditions with REQUIRE or CHECK macros. To prepare for writing tests with Catch2, define a mock API request function to simulate API responses and override the actual httpRequest function with the mock function for testing, using a preprocessor macro for conditional compilation.