Makers Blog
3,105 views 0 comments

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

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.

Discussion

Please login/register to comment

Login/Register

Leave a Reply

Newsletter
Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users