GitLab Blog

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

thumbnail

Setting up Unit Testing with Catch2 and GitLab CI

Set up the application for testing

  • Compilation and building of the air quality monitoring application
  • Running the program with a U.S zip code

Install Catch2

  • Adding Catch2 for testing
  • Creating test folder and test files
  • Updating CMake configuration

Write tests in Catch2

  • Defining functions for mock API requests
  • Test 1: Verify API key retrieval
  • Test 2: Geocode the zip code
  • Test 3: Air quality API test

Build and run the tests

  • Building and compiling the application
  • Running the tests to check for pass or fail
  • Adding new files to Git, committing, and pushing changes

View the test report

  • Reviewing test results in GitLab's Pipeline view

Simulate a test failure

  • Introducing a bug in the code to cause a test failure
  • Adding a new test case to check for correct parsing of air quality data

Setting up Unit Testing with Catch2 and GitLab CI

Set up the application for testing

  • Compile and build the air quality monitoring application
  • Execute the program with a U.S zip code input

Install Catch2

  • Add Catch2 for testing functionalities
  • Establish test directory and files
  • Update CMake config for integration

Write tests in Catch2

  • Create functions for mock API requests
  • Implement test 1: API key retrieval verification
  • Include test 2: Geocoding for zip code
  • Integrate test 3: Air quality API testing

Build and run the tests

  • Compile the application and build binaries
  • Run the tests to validate functionality
  • Commit new files to Git and push changes

View the test report

  • Check test outcomes in GitLab's Pipeline view

Simulate a test failure

  • Intentionally introduce a bug for test failure
  • Add a new test case for validating air quality data parsing