Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Industrial Technology

Master C++ Unit Testing with Catch2: A Practical Guide

Abstract

In this article I will explain how to use catch2 to do unit testing.

How to build

To build the sources, you have to download a release from https://github.com/catchorg/Catch2/tags. Now you can compile the source with gcc. The following code-snippet will show the cmake script to compile.


#Please set the environment variables to your needs cmake \ -G "Unix Malkefiles" \ -D CMAKE_BUILD_TYPE=Release \ -D BUILD_TESTING=OFF \ -D BUILD_SHARED_LIBS=ON \ -D "CMAKE_STAGING_PREFIX=${CMAKE_STAGING_PREFIX}" \ -D "CMAKE_INSTALL_PREFIX=${CMAKE_STAGING_PREFIX}" \ -D BUILD_TESTS=OFF \ -S "${SOURCE_DIRECTORY}" \ -B "${BUILD_DIRECTORY}" cmake --build "${BUILD_DIRECTORY}" --target install

Integrate into project structure

Recommended project structure:

|-- project
|----CMakeLists-txt
|----test
|------cmake
|--------Catch.cmake
|--------CatchAddTests.cmake
|--------FindCatch2.cmake
|--------ParseAndAddCatchTests.cmake
|------Main.cpp
|------ExampleTest1.cpp
|------ExampleTest2.cpp
|------CMakeLists.txt

Settings in CMakeLists.txt of the project


include(CTest)

target_compile_definitions(${TARGET} PRIVATE
    TEST_DATA_PATH="${PROJECT_SOURCE_DIR}/data")

//set environment variable to ON if you want to enable testing
if (BUILD_TESTING)
    enable_testing()
    add_subdirectory(test)
endif()

Settings in CMakeLists.txt of the test directory.


cmake_minimum_required(VERSION 3.13)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/test/cmake")

set (WILDCARD_SOURCE *.cpp)

file(GLOB_RECURSE TEST_SOURCES ${WILDCARD_SOURCE})

add_executable(${TARGET_TEST} ${TEST_SOURCES})

find_package(Catch2 REQUIRED)

# Link to the desired libraries
target_link_libraries(${TARGET_TEST}
    PRIVATE
    Catch2::Catch2
    ...
)

target_compile_definitions(DcmlParserTest PRIVATE
    TEST_DATA_PATH="${PROJECT_SOURCE_DIR}/data")

include(ParseAndAddCatchTests)

ParseAndAddCatchTests(${TARGET_TEST}) 

How to use

Main.cpp


#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

ExampleTest.cpp

This example shows how to test with SCENARIOS.


#include <catch2/catch.hpp>

#ifndef DATA_PATH
#define DATA_PATH "/tmp/data"
#endif

SCENARIO("testcase", "")
{
    GIVEN("usercase 1")
    {
        WHEN("instance is created")
        {
            THEN("test properties")
            {

                //check if true
                REQUIRE(...);

                /check if no exception
                REQUIRE_NOTHROW(...);

            }
        }
    }
}

ExampleTest2.cpp

This example shows how to test with TEST_CASE.


#include <catch2/catch.hpp>

#ifndef DATA_PATH
#define DATA_PATH "/tmp/data"
#endif

TEST_CASE( "TestCase1", "" ) {

    REQUIRE( 1 == 1 );

}

TEST_CASE( "Testcase2", "" ) {

    REQUIRE( 3 != 1 );

}

Special note to PLCnext applications

In order to use catch2 as testing framework you have to exclude ARP content. You have to compile your code with the local gcc compiler. With this setup you can test your non ARP code locally.

More Information

If you are interested in getting more information about catch2 you can check the following link:

License

The library is published under Boost Software License 1.0


Industrial Technology

  1. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  2. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  3. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  4. C++ Operator Overloading – A Practical Guide with Code Examples
  5. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  6. C++ Structs Explained with a Practical Example
  7. C++ Classes & Objects: A Practical Guide with Code Examples
  8. C++ Polymorphism Explained: Practical Examples & Key Concepts
  9. Mastering std::list in C++: Syntax, Functions & Practical Examples
  10. Automating Electronic Product Testing with Robotics: Boost Efficiency & Quality