No tests found while running gtest - c++

I was making use of gtest to test my c++ project. I followed the steps from here
Everything went well but when I ran the cd build && ctest command i'm getting the output as Test project C:/Users/admin/Desktop/last/build No tests were found!!!
My CMakeLists.txt file
cmake_minimum_required(VERSION 3.14)
project(last)
# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
fact.cpp
header.h
main.cpp
testt.cpp
)
target_link_libraries(
hello_test
gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)

I changed a line in my CMakeLists file
add_executable(
hello_test
header.h
main.cpp
)
And added a line
add_test(testt.cpp hello_test)
This gave me the output correctly and ran the tests

Related

GoogleTest gtest_discover_test could not find test

I'm trying to add google test to my project and keep getting
"[build] get_property could not find TARGET testcolor. Perhaps it has not yet been
"
This is my cmake file:
cmake_minimum_required(VERSION 3.13)
project(RAYTRACE)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
include(CTest)
enable_testing()#Redundent I know
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
include_directories("<...>/googletest-src/googletest/include")
##GTEST##
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
set(SOURCE_FILES
main.cpp
src/Utils/testcolor.cc
src/Utils/testcolor
)
add_executable(RAYTRACE ${SOURCE_FILES})
target_link_libraries(RAYTRACE
PRIVATE
testcolor
GTest::gtest_main
GTest::gtest
)
include(GoogleTest)
gtest_discover_tests(testcolor)
Without the last line gtest is built properly and the gtest.h header is included in "src/Utils/testcolor.cc" and detected.
yet I cant add the test.
I solved the problem by following the This
guide online which made me undersand my many many mistakes configuring CMake and GTest. Thanks!

CMake not including library files when I use FetchContent

I'm working on a project where the CMake structure looks like this:
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_CXX_STANDARD 20)
project(overflow VERSION 0.1.0)
add_subdirectory(src)
enable_testing()
include(CTest)
add_subdirectory(test)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
src/CMakeLists.txt
find_package(Boost REQUIRED)
set(HEADER_FILES overflow.h)
include_directories(${Boost_INCLUDE_DIRS})
add_library(overflow ${HEADER_FILES})
link_libraries(overflow PRIVATE Boost::uuid) # Fails with `target_`
These work fine. However, I get an error when I set up GTest as shown below in my test directory.
test/CMakeLists.txt
## Setup GTest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip
)
### For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_executable(overflowtest overflow.i.t.cpp)
target_link_libraries(overflowtest PRIVATE
overflow
gtest
gtest_main)
include(GoogleTest)
gtest_discover_tests(overflowtest)
CMake builds correctly, but when I run the test, the error in overflow.h is:
boost/uuid/uuid.hpp: No such file or directory
What I find strange is that if I change my settings so that test/CMakeLists.txt looks like:
test/CMakeLists.txt
find_package(GTest CONFIG REQUIRED)
add_executable(overflowtest overflow.i.t.cpp)
target_link_libraries(overflowtest PRIVATE
overflow
GTest::gtest
GTest::gtest_main)
There are no errors and it runs correctly. Can someone please explain why using the FetchContent method recommended by Google seems to break my project?

How to add multiple source files to the test executable in googletest without redefinition errors

I am new to googletest and CMake. I wanted to learn googletest so I decided to try writing and running some tests on a collection of files I wrote a while back.
My directory looks like:
-CMakeLists.txt
-normTest1.cpp
-deathTest1.cpp
-normTest2.cpp
-deathTest2.cpp
-build
-[all CMake files like Makefile, cache, bin, lib, etc.]
-my_library_dir
-obj1.h
-obj2.h
-obj1.cpp
-obj2.cpp
-my_library.h
Now my CMakeList.txt is: (based on this tutorial: https://google.github.io/googletest/quickstart-cmake.html)
cmake_minimum_required(VERSION 3.14)
project(my-library-testing)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
set(${PROJECT_NAME}_CXX_FLAGS "${${PROJECT_NAME}_CXX_FLAGS} -std=c++17")
set(${PROJECT_NAME}_CMAKE_CXX_FLAGS "${${PROJECT_NAME}_CMAKE_CXX_FLAGS} -std=c++17")
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
library_test
normTest1.cpp
normTest2.cpp
deathTest1.cpp
deathTest2.cpp
)
target_link_libraries(
library_test
gtest_main
)
include(GoogleTest)
gtest_discover_tests(library_test)
and each of my .*Test[12].cpp files have an #include <gtest/gtest.h> and a #include "my_library_dir/my_library.h" in them.
Unfortunately, executing cmake -S . -B build and then cmake --build build gives me a bunch of linker errors because of multiple definitions of functions in the source files of my_libary_dir across each *.o file. How can I edit my CMakeLists.txt file to get around these linker errors? Temporarily, I am only putting one source file in the add_executable(...) and instead #includeing the other source files in the one source file that I include in add_executable.

Why does "#include <gtest/gtest.h>" fail even after having used "FetchContent_MakeAvailable" in my CMake file?

This is my first time attempting the Google Test API in C++ and one of my first experiences with CMake. If it's useful, I'm using CLion. My CMake file, shown below, should ostensibly allow for files to #include <gtest/gtest.h>:
cmake_minimum_required(VERSION 3.19)
project(chord)
# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread")
add_executable(
chord
main.cpp
src/node.h
src/node.cpp
src/log.h
src/log.cpp
src/sha1.h
src/sha1.cpp
src/messageParser.h
src/messageParser.cpp
src/fingerTable.h
src/fingerTable.cpp
src/key.h
src/key.cpp)
include(GoogleTest)
enable_testing()
add_executable(
unitTests
test/fingerTableTests.cc
test/keyTests.cc
test/logTests.cc
test/messageParserTests.cc
test/nodeTests.cc
test/messageParserTests.cc
)
target_link_libraries(
unitTests
gtest_main
)
include(GoogleTest)
gtest_discover_tests(unitTests)
However, when I attempt #include <gtest/gtest.h> from main.cpp, I receive the error gtest/gtest.h: No such file or directory. Can someone pinpoint the error in my CMake file? Thanks and sorry for the trouble.
Try to also link gtest:
target_link_libraries(
unitTests
gtest_main
gtest
)

Error running google test on default in CLion

So, unfortunatly I am unsure how to properly describe the error message.
Essentially I am trying to get used to google test, - I want to use it to test my C++ project in CLion. I create a new library project, with the following classes:
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
and also:
#include "gtest/gtest.h"
TEST(MyTestCategory, Vec2DAdditionTest){
EXPECT_EQ(1, 1);
}
Of course these tests are not useful at all - but its just to see if everything works the way it should.
Now when I tried to run them, I am prompted the following error:
6:46 PM Error running 'MyTestCategory.Vec2DAdditionTest': Cannot run 'MyTestCategory.Vec2DAdditionTest' on '<default>'
6:47 PM Error running 'All in main.cpp': Cannot run 'All in main.cpp' on '<default>'
What am I missing? I can't get the tests to run, - neither individually, nor directly over the main function?
Also after this, the build/run button gets greyed out in CLion and I have to right click on the main.cpp to force it to run/compile..
Essential I have a project structure like so:
src/
a.cpp
b.cpp
CMakeLists.txt
test/
main.cpp
atests.cpp
CmakeLists.txt
CMakeLists.txt
My run configuration for the test project looks like so:
Here is an example on how you can add GTests in your CLion project:
Consider a project structure very similar to what you have presented, however, with an additional file CMakeLists.txt.in in the test folder.:
src/
a.cpp
b.cpp
CMakeLists.txt
test/
main.cpp
atests.cpp
CMakeLists.txt
CMakeLists.txt.in
CMakeLists.txt
The CMakeLists.txt.in helps to download and add the GTest libraries to your project while building your project.
The content of CMakeLists.txt.in looks as below:
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 ""
)
The test/CMakeLists.txt file looks as below:
cmake_minimum_required(VERSION 3.10)
### START OF CONFIGURING GTEST
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 )
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
# 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)
### END OF CONFIGURING GTEST
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(test_${PROJECT_NAME} main.cpp atest.cpp)
target_link_libraries(test_${PROJECT_NAME} ${PROJECT_NAME} gtest gtest_main)
add_test(NAME test_PROJECT_NAME COMMAND test_${PROJECT_NAME})
CMakeLists.txt in the root folder has the following content:
cmake_minimum_required(VERSION 3.10)
project(gtestTest)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
add_subdirectory(test)
Now reload the CMake configuration and try running the test_gtestTest target to run the unit tests.
You can also create custom run configurations using gtest template of CLion to get user friendly test reports.
For more information on GTests with CLion, please refer to:
Google Test Support in Clion
Building Google Tests with CMake