2

I've been trying to set up google test v1.10 in visual studio 2019 for a really long time now and it hasn't been working, and a lot has changed as well. Most of the tutorials are out of data and Microsoft help page says to use the google test adapter but I have an already existing project that I want to add google test. I've referred to the google test installation page(https://github.com/google/googletest/blob/master/googletest/README.md) and tried to set it up using the cmake method but it isn't working and the cmake method is a bit complicated. I was wondering if anyone could help with the steps for a dummy like me? I feel like this will also help people who are trying to set it up currently and are stuck as well.

This is what i did for the cmake thingy:

  1. created an empty project in vs, and added a source.cpp file and a Header.h file(for the example).

  2. I created a CMakeLists.txt and a CMakeLists.txt.in file and copy pasted this in for the first one(tweaked a bit for my project)

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

# Now simply link against gtest or gtest_main as needed. Eg
add_executable(test_ source.cpp)
target_link_libraries(test_ gtest_main)
add_test(NAME example_test COMMAND test)

and CMakeLists.txt.in

cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)
  1. I created a build folder in my project file
  2. I used the cmake command from the terminal to build it into the build folder like this
cmake -S "path to src code" -B "path to build"

and these are the warnings I got:

No project() command is present. The top level CMakeLists.txt file must contain a literal, direct call to the project() command. And a line of code such as

project(projectName)

near the top of the CMake file, but after cmake_minimum_required().

CMake is pretending there is a "project(projectName)" command on the first line.

This warning is for project developers. Use who-dev to supress it.

other than that it did the building and this is my project tree currently enter image description here

and this is what's there in /build

enter image description here

but idk what to do after this? How do I start creating tests? If possible i want to create another project inside my current solution so i can run tests separately

here's what is mean

enter image description here

the googletest project is where i want to run my tests if possible

and this what my project tree looks like after

enter image description here

JerSci
  • 167
  • 1
  • 9
  • What wasn't working? What error messages did you receive at what point in the process? – starturtle Oct 30 '20 at 14:02
  • @starturtle i added more info, could you help please? – JerSci Oct 30 '20 at 17:44
  • What I'm missing is the `enable_testing()` call, for example. Also, the beginning of your own CMakeLists.txt should contain a `cmake_minimum_required(VERSION 2.8.2)`(or whatever you need) and a `project(Project24)` call (that's your warning). Your add_executable call should rather be `add_executable(myTest test_source.cpp)`, changing `add_test(NAME example_test COMMAND myTest)`. That's what I can see from here so far. I'm sadly only familiar with either CMake or gtest, not with both in the same project. – starturtle Oct 30 '20 at 18:12

1 Answers1

2

Follow this link to the T: https://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php You won't have to use CMake.

In VS2019, in order to get win32 static lib and console applications, in the create a new project section, select C++ in language tag, Windows in platform tag, and All Project Types in the types tag. Then scroll down to find Windows Desktop Wizard and select it. Rest of it is according to the tutorial.

JerSci
  • 167
  • 1
  • 9
  • Since you have a workaround about your issue, I suggest you could [mark your own answer](https://stackoverflow.blog/2009/01/06/accept-your-own-answers/) to help other community members search and handle similar issues. Thanks:) – Mr Qian Nov 02 '20 at 08:15
  • thanks for the heads up @PerryQian-MSFT . Did it end up working for you too? – JerSci Nov 02 '20 at 08:36