google test with visual studio 2019 and cmake
Asked Answered
P

1

9

I am trying to setup google test with visual studio 2019 and cmake.

This is my CMakeFileLists.txt content:

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 )

My tests.cpp file looks like this:

#include <gtest/gtest.h>

TEST(ABC, TEST1) {
    EXPECT_EQ(true, true);
}

TEST(ABC, TEST2) {
    ASSERT_TRUE(2 == 2);
}

this minimal example is from another stackoverflow question: CMake file for integrated Visual Studio unit testing

Thats what I get after I have build the application: enter image description here

a single Test with the name runUnitTests. However in the picture of the answer from the question above I would expect to see the name of each test function. Something like this:

runUnitTests
- ABC
  - TEST1
  - TEST2

I have tested it with a new visual studio solution and added a google unit test project. Pasting the test functions into this projects results into this picture:

enter image description here

So this works fine. It must have something to do with the open a local folder method which I am using to handle my cmake project.

Pleopod answered 9/6, 2019 at 0:31 Comment(1)
I assume the question is "how to get test log output in studio 2019"?Janejanean
D
3

Here is a possible answer to your question:

cmake_minimum_required(VERSION 3.10)

project(test_me)

enable_testing()

find_package(GTest REQUIRED)
include(GoogleTest)

add_executable(runUnitTests tests.cpp)
target_link_libraries(runUnitTests GTest::GTest GTest::Main)

gtest_discover_tests(runUnitTests)

The command that discovers the full list of tests in your test binary is gtest_discover_tests(), and it is part of the GoogleTest CMake module (you can see the docs locally with cmake --help-module GoogleTest). This module has been introduced with CMake 3.9, but the command gtest_discover_tests() was only added in CMake 3.10. You should note, however, that if your test binary has many test cases, this will slow things down significantly.

Dinesen answered 30/6, 2021 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.