/google_test_exmaple

C++ testing example.

Primary LanguageCMake

Google Test Example

# google test
include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)


## testing
enable_testing()

add_executable(
        hello_test
        hello_test.cc
)
target_link_libraries(
        hello_test
        GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)

Quick Start

  1. Add scripts to CmakeLists.txt
    # google test
    include(FetchContent)
    FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
    )
    # For Windows: Prevent overriding the parent project's compiler/linker settings
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(googletest)
    
    
    ## testing
    enable_testing()
    
    add_executable( # TODO: Change this
    hello_test  
    hello_test.cc
    )
    target_link_libraries(
    hello_test  # TODO: Change this
    GTest::gtest_main
    )
    
    include(GoogleTest)
    gtest_discover_tests(hello_test)  # TODO: Change this
  2. Add test file(e.g. hello_test.cc)
    #include <gtest/gtest.h>
    
    // Demonstrate some basic assertions.
    TEST(HelloTest, BasicAssertions) {
    // Expect two strings not to be equal.
    EXPECT_STRNE("hello", "world");
    // Expect equality.
    EXPECT_EQ(7 * 6, 42);
    }
  3. Build and test
    cmake -S . -B build
    cmake --build build
    cd build && ctest

Reference