I want to run the sample tests and debug the Google Test project. I am using VS Code on Ubuntu 16.04 LTS.
/home/user/Desktop/projects/cpp/googletest
,mybuild
at /home/user/Desktop/projects/cpp/mybuild
.cmake -Dgtest_build_samples=ON /home/user/Desktop/projects/cpp/googletest
to build the project and this generated a bunch of files and apparently the build succeeded.Now, I have 2 problems:
How do I run the sample tests for the project?
How do I debug these test and the source code for the project?
Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.
From the Command Palette, by running any of the following commands: Test: Run All Tests - Runs all tests that have been discovered. Test: Run Tests in Current File - Runs all tests in a file that that is open in the editor. Test: Run Test at Cursor - Runs only the test method under your cursor in the editor.
Run Apex Tests In Visual Studio Code, click the View menu then choose Command Palette.... Alternatively, you can use the keyboard shortcut Ctrl+Shift+P (Windows or Linux) or Cmd+Shift+P (macOS) to open the Command Palette. Enter apex test in the search box, then choose SFDX: Run Apex Tests.
/home/user/Desktop/projects/cpp/ # your project lives here
└─cpp/
├─ CMakeLists.txt
├─ myfunctions.h
└─ mytests.cpp
googletest
to this directory:└─cpp/
├─ googletest/
├─ CMakeLists.txt
├─ myfunctions.h
└─ mytests.cpp
CMakeLists.txt
and enter the following:cmake_minimum_required(VERSION 3.12) # version can be different
project(my_cpp_project) #name of your project
add_subdirectory(googletest) # add googletest subdirectory
include_directories(googletest/include) # this is so we can #include <gtest/gtest.h>
add_executable(mytests mytests.cpp) # add this executable
target_link_libraries(mytests PRIVATE gtest) # link google test to this executable
myfunctions.h
for the example:#ifndef _ADD_H
#define _ADD_H
int add(int a, int b)
{
return a + b;
}
#endif
mytests.cpp
for the example:#include <gtest/gtest.h>
#include "myfunctions.h"
TEST(myfunctions, add)
{
GTEST_ASSERT_EQ(add(10, 22), 32);
}
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Now you just have to run the tests. There are multiple ways to do that.
In the terminal, create a build/
dir in cpp/
:
mkdir build
Your directory should now look like this:
└─cpp/
├─ build/
├─ googletest/
├─ CMakeLists.txt
├─ myfunctions.h
└─ mytests.cpp
Next go into the build
directory:
cd build
Then run:
cmake ..
make
./mytests
Alternative way:
CMake Tools
extension for VS Code/home/user/Desktop/projects/cpp/googletest
build/
inside it so that it looks like following:└─cpp/googletest/
├─ build/
├─ ...other googletest files
cd build
cmake -Dgtest_build_samples=ON -DCMAKE_BUILD_TYPE=Debug ..
make -j4
./googletest/sample1_unittest
googletest
folder into VS Code.vscode
directory. Inside it is settings.json
file, open it, and add the following to it: "cmake.configureSettings": { "gtest_build_samples": "ON" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With