存储PLCNext社区PLCNext. on LinkedInPLCNext. on Instagram  PLCNext. on YouTube github plcnext社区存储PLCNext社区

 

 如何创建博客条目

制造商博客
Hits: 1289

C ++使用Catch2进行测试

抽象的

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

如何建设

要构建来源,您必须从中下载版本 //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

融入项目结构

推荐的项目结构:

|-- 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}) 

如何使用

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("采用rcase 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 );

}

PLCNext应用程序的特殊注释

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.

更多信息

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

执照

图书馆发布 提升软件许可证1.0