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:
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:
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.
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.
Related
I have a folder structure like
and I am trying to get Catch2 setup, my CMake files look like:
the topmost CMake:
cmake_minimum_required(VERSION 3.21)
project(throwaway)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
add_subdirectory(tests)
add_executable(foo_main main.cpp)
target_link_libraries(foo_main PUBLIC foo_lib)
my src CMake:
add_subdirectory(foo)
my src/foo CMake:
add_library(foo_lib Foo.cpp)
my tests CMake:
add_subdirectory(foo)
add_executable(foo_test catch_runner.cpp)
target_link_libraries(foo_test PUBLIC
foo_test_lib)
my tests/foo CMake
add_library(foo_test_lib
FooTests.cpp)
target_link_libraries(foo_test_lib PUBLIC
foo_lib)
From there, I used Clion's Catch2 integration to set up my run config as
so nothing crazy here
However, I get this error:
I discovered that the error goes away if I edit the tests CMake into
add_executable(foo_test catch_runner.cpp foo/FooTests.cpp)
target_link_libraries(foo_test PUBLIC foo_lib)
and the test works as expected. But I obviously don't want to manually add each file into an executable, I want to be able to make a library that I can just slap the catch_runner into.
I have no clue why the test doesn't work when I link it as a library, but works when I add it manually. Any ideas?
Figured out it from the links at How do you add separate test files with Catch2 and CMake?
in my tests/foo CMake I changed it to
add_library(foo_test_lib OBJECT
FooTests.cpp)
target_link_libraries(foo_test_lib PUBLIC
foo_lib)
I want the c++ unit test written in google test displayed in the VS 2019 test explorer.
The tests are setup correctly and can be executed. The results are shown in the VS debug console/commandline-like window. No error messages besides the test dependent messages are shown.
I want start the tests form the test explorer and want to create test play lists.
I installed the google test adapter provided by the VS Installer. I followed guidelines and the suggested troubleshooting at TestAdapterForGoogleTest.
Does another way exist to get the google test to be dispalyed in the test explorer?
What are other known incompatibilities with google test and the VS test explorer?
I had the same problem as you. I'm having a main CMakeLists.txt, and another two CMakeLists.txt files in the subdirectories: one for the static library I'm testing and one for the test project itself.
For making sure the tests appear in the Test Explorer I had to move enable_testing() from the test subdirectory into the main CMakeLists.txt.
option(MY_PROJECT_TESTS "Build unit tests" ON)
if(MY_PROJECT_TESTS)
enable_testing()
add_subdirectory("test")
endif()
Then in the test subdirectory, I'm setting up the GoogleTest environment, and adding test the following way:
set(GTEST_DIR "googletest/googletest" CACHE PATH "gtest directory")
include(GoogleTest)
set(gtest_force_shared_crt OFF CACHE BOOL "" FORCE)
add_subdirectory("googletest")
project(My_project_test)
if (WIN32)
add_library(qtpcre STATIC IMPORTED)
set_target_properties(qtpcre PROPERTIES
IMPORTED_LOCATION_DEBUG ${QT5_DIR}/lib/qtpcre2d.lib
IMPORTED_LOCATION_RELEASE ${QT5_DIR}/lib/qtpcre2.lib
)
endif()
set(CommonTestLib
Qt5::Core
My_project
gtest_main
)
if (WIN32)
list(APPEND CommonTestLib
Ws2_32.lib
version.lib
Netapi32.lib
Userenv.lib
Crypt32.lib
Winmm.lib
qtpcre
)
endif()
add_executable (My_project_test test_main.cpp test_cases.cpp)
target_precompile_headers(My_project_test REUSE_FROM My_project)
target_link_libraries(My_project_test ${CommonTestLib})
gtest_add_tests(TARGET My_project_test EXTRA_ARGS --arg1 "${CMAKE_CURRENT_SOURCE_DIR}/data")
The very last line is important. Instead of gtest_add_tests, you can also use add_test. It needs different parameters but that works too when your goal is showing test cases in VS2019's Test Explorer.
The reason the above solution helped:
When you add enable_testing() to your top-level CMakeLists.txt file, it will generate a top-level CTestTestfile.cmake file in your build directory. This is needed by the Test Explorer to roll up all the test cases generated during the build process. If you've got a certain CMake hierarchy within your code structure you should have a similar one for CTest.
My top-level CTestTestfile.cmake file content:
# CMake generated Testfile for
# Source directory: C:/Projects/myproject
# Build directory: C:/Projects/myproject/out/build/x86-Debug
#
# This file includes the relevant testing commands required for
# testing this directory and lists subdirectories to be tested as well.
subdirs("test")
The lower level CTestTestfile.cmake file content:
# CMake generated Testfile for
# Source directory: C:/Projects/MyProject/test
# Build directory: C:/Projects/MyProject/out/build/x86-Debug/test
#
# This file includes the relevant testing commands required for
# testing this directory and lists subdirectories to be tested as well.
add_test(Environment.TestCommandLineArgument "C:/Projects/MyProject/out/build/x86-Debug/test/MyProject_test.exe" "--gtest_filter=Environment.TestCommandLineArgument" "--arg1" "C:/Projects/MyProject/test/data/")
set_tests_properties(Environment.TestCommandLineArgument PROPERTIES _BACKTRACE_TRIPLES "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.19/Modules/GoogleTest.cmake;380;add_test;C:/Projects/MyProject/test/CMakeLists.txt;38;gtest_add_tests;C:/Projects/MyProject/test/CMakeLists.txt;0;")
add_test(MyProjectExampleCreatorDevice.TestCreateExampleImage "C:/Projects/MyProject/out/build/x86-Debug/test/MyProject_test.exe" "--gtest_filter=MyProjectExampleCreatorDevice.TestCreateExampleImage" "--arg1" "C:/Projects/MyProject/test/data/")
set_tests_properties(MyProjectExampleCreatorDevice.TestCreateExampleImage PROPERTIES _BACKTRACE_TRIPLES "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.19/Modules/GoogleTest.cmake;380;add_test;C:/Projects/MyProject/test/CMakeLists.txt;38;gtest_add_tests;C:/Projects/MyProject/test/CMakeLists.txt;0;")
...
for me upgrading from 1.8.1.3 to 1.8.1.4 using nuget broke my project.
It switched
Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3
to
Microsoft.googletest.v140.windesktop.msvcstl.dyn.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.dyn.rt-dyn.targets"
revert back to the previous worked for me. (test explorer is now back) I think just making sure you use the static version as opposed to the dynamic should work as well.
There are multiple extensions that seem like they should be used.
If you follow the advice of https://learn.microsoft.com/en-us/visualstudio/test/how-to-use-google-test-for-cpp?view=vs-2022
you use the TestAdapterForGoogleTest from microsoft ? ()
There is another from Google. (Google test adapter). I have a project that used old versions of gtest. I installed this extension and things stopped working.
uninstall it and restart. rebuild and things seemed to work again.
I have been looking at examples of CMake to help me build my project with its test folder. The main issue is that I have to include all the test/.cpp* files in my test folder into tests/main.cpp for the tests to run. I think I should include my tests with the add_test call in my test/CMakeLists.txt. Below is my current CMakeLists file:
enable_testing()
find_package(GTest REQUIRED)
add_executable(test main.cpp)
target_include_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
target_link_libraries(test MainProjectLib ${GTEST_LIBRARIES})
And my main.cpp is the following.
#include "file_in_test1.cpp"
#include "file_in_test2.cpp"
#include <gtest/gtest.h>
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I read some examples that use the standard CTest library for unit testing, but I am using Google Test. I would like to be able to run cmake, make, and then ./test to run all test methods found in file_in_test1.cpp and file_in_test2.cpp without having to directly import those files into test/main.cpp. What additions to CMakeLists.txt do I have to make?
The one line you could add at the end would be:
gtest_discover_tests(test)
This is a replacement for gtest_add_tests since cmake version 3.10, and then after you build everything, you can just run ctest and it will give you a nice summary of the tests it just ran. You can read more about this option here.
Also, just as a note, it would probably be better to make individual test files for each of the tests you have, rather than including the .cpp files in your main test file.
What I generally do is define an option to allow for testing and a function that creates the test I need (see below):
option(build_all_tests "Build all unit tests in the test directory." OFF)
if (build_all_tests)
include(CTest)
include(GoogleTest)
enable_testing()
## Function to create a new test based off the pre-defined naming template ##
function(new_test testname interiorDirectory)
add_executable(${testname} ${interiorDirectory}/${testname}.cpp)
target_link_libraries(${testname} ${GTEST_LIBRARIES})
gtest_discover_tests(${testname}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${interiorDirectory})
endfunction(new_test)
## Locate GTest ##
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
## Create all tests ##
new_test(test1 test/test1)
new_test(test2 test/test2)
...
endif()
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 use the integrated IDE testing, for example using either google test or the native microsoft unit testing framework? All of Microsoft's examples unfortunately just create the project in Visual Studio, rather than starting from a CMake file.
Any help is appreciated, thanks!
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?
Although this is a late reply, but it's the latest answer for any reference.
For Microsoft Unit Testing Framework, the following simplified files work with me:
CMakeLists.txt
set(UNIT_TEST_TARGET_NAME my_lib)
add_library(${UNIT_TEST_TARGET_NAME} SHARED)
target_sources(${UNIT_TEST_TARGET_NAME} PRIVATE
tests.cpp
tests.h
)
target_link_libraries(${UNIT_TEST_TARGET_NAME}
PRIVATE MSUnitTestFramework::MSUnitTestFramework)
enable_testing()
find_program(VSTest_EXECUTABLE NAME vstest.console.exe REQUIRED)
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(MSUnitTestFramework REQUIRED)
add_test(NAME ${UNIT_TEST_TARGET_NAME}
COMMAND "${VSTest_EXECUTABLE}" "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${UNIT_TEST_TARGET_NAME}.dll"
)
/cmake/FindMSUnitTestFramework.cmake
add_library(MSUnitTestFramework::MSUnitTestFramework SHARED IMPORTED)
set_target_properties(MSUnitTestFramework::MSUnitTestFramework PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "$ENV{VCInstallDir}Auxiliary/VS/UnitTest/include"
IMPORTED_IMPLIB "$ENV{VCInstallDir}Auxiliary/VS/UnitTest/lib/x86/Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib"
)
set(MSUnitTestFramework_FOUND TRUE)
tests.cpp
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace my_unit_test
{
TEST_CLASS(my_tests)
{
public:
TEST_METHOD(test1)
.
.
You can Run All Tests and so on from MSVC.
BTW, instead of add_test for the whole DLL, you can use it to add test by test.
A typical 'CMakeLists.txt' included with a VTK wiki example is shown below;
cmake_minimum_required(VERSION 2.8)
PROJECT(Arrow)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(Arrow MACOSX_BUNDLE Arrow)
if(VTK_LIBRARIES)
target_link_libraries(Arrow ${VTK_LIBRARIES})
else()
target_link_libraries(Arrow vtkHybrid)
endif()
I'm able to build the examples successfully in Windows 7 and Visual Studio 2012.
I would like to write a 'CMakeLists.txt' file so that I can build/run a test class (i.e. This one ReebGraph/Testing). I'm assuming I'm right in saying that they require different kinds of make files. The 'CMakeLists.txt' for 'TestReebGraph.cxx' is shown below.
vtk_add_test_cxx(TestReebGraph.cxx NO_DATA NO_VALID NO_OUTPUT)
vtk_test_cxx_executable(${vtk-module}CxxTests)
How would I write one for the testing class? Do I somehow have to merge the two?
The cmake commands vtk_* are for writing tests inside of the VTK CMake system. What you'll want is to first enable_testing() in your CMakeLists.txt. Then you'll have to change the function TestReebGraph to be called 'main' in TestReebGraph.cxx. You can then use add_test(TestReebGraph TestReebGraph.cxx) to build the test that you have presumably copied into your project directory.