CMake file for integrated Visual Studio unit testing

前端 未结 1 550
闹比i
闹比i 2021-01-07 02:01

Visual Studio 2017 has integrated C++ unit testing (native, google test, ctest, etc.). How can I create a CMakeLists.txt file that will create a project like this that will

相关标签:
1条回答
  • 2021-01-07 02:22

    Mikewho,

    I setup a small example using Google Test project that works with integrated IDE testing.

    Create an empty directory and save these two files:

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.0)
    project(test_me)
    
    # GTest
    enable_testing()
    find_package(GTest REQUIRED)
    include_directories(${GTEST_INCLUDE_DIRS})
    
    # Unit Tests
    # Add test cpp file
    add_executable( runUnitTests tests.cpp)
    # Link test executable against gtest & gtest_main
    target_link_libraries(runUnitTests ${GTEST_BOTH_LIBRARIES})
    add_test( runUnitTests runUnitTests )
    

    tests.cpp

    #include <gtest/gtest.h>
    
    TEST(ABC, TEST1) {
      EXPECT_EQ(true, true);
    }
    

    The in a command prompt type

    mkdir build
    cd build
    cmake .. "-DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"
    

    Note: I had vcpkg install gtest

    C:\dev\vcpkg>vcpkg.exe install gtest
    

    Make sure you have this installed in Visual Studio 2017

    In Tools > Options > Test Adapter for Google Test set the regex to .exe

    Build the solution and press Run all in the Test Explorer

    The first time it runs it will find the test case

    [12/3/2018 8:38:41 AM Informational] ------ Run test started ------
    [12/3/2018 8:38:42 AM Warning] Could not locate debug symbols for 'C:\dev\cpptests\GoogleTest\build\Debug\runUnitTests.exe'. To make use of '--list_content' discovery, ensure that debug symbols are available or make use of '<ForceListContent>' via a .runsettings file.
    [12/3/2018 8:38:42 AM Informational] Test Adapter for Google Test: Test execution starting...
    **[12/3/2018 8:38:42 AM Informational] Found 1 tests in executable** C:\dev\cpptests\GoogleTest\build\Debug\runUnitTests.exe
    [12/3/2018 8:38:42 AM Informational] Running 1 tests...
    [12/3/2018 8:38:42 AM Informational] Google Test execution completed, overall duration: 00:00:00.2390446
    [12/3/2018 8:38:42 AM Informational] ========== Run test finished: 1 run (0:00:01.2668844) ==========
    

    I hope this helps?

    0 讨论(0)
提交回复
热议问题