How to run tests and debug Google Test project in VS Code?
Asked Answered
P

1

34

I want to run the sample tests and debug the Google Test project. I am using VS Code on Ubuntu 16.04 LTS.

  • I cloned the project locally at /home/user/Desktop/projects/cpp/googletest,
  • created a new directory called mybuild at /home/user/Desktop/projects/cpp/mybuild.
  • According to the README instructions here: https://github.com/google/googletest/tree/master/googletest I used the command, 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:

  1. How do I run the sample tests for the project?

  2. How do I debug these test and the source code for the project?

Pahari answered 15/7, 2020 at 8:39 Comment(0)
O
59
  1. Start with a clean directory:
/home/user/Desktop/projects/cpp/ # your project lives here
  1. Add your cmake file(CMakeLists.txt), your source files, and test file. The directory now looks like this:
└─cpp/
    ├─ CMakeLists.txt
    ├─ myfunctions.h
    └─ mytests.cpp
  1. Clone and add googletest to this directory:
└─cpp/
    ├─ googletest/
    ├─ CMakeLists.txt
    ├─ myfunctions.h
    └─ mytests.cpp
  1. Open your CMakeLists.txt and enter the following:
cmake_minimum_required(VERSION 3.12) # version can be different

project(my_cpp_project) #name of your project

enable_testing() #to  discover tests in test explorer 

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

include(GoogleTest)
gtest_discover_tests(mytests) # discovers tests by asking the compiled test executable to enumerate its tests
  1. Contents of myfunctions.h for the example:
#ifndef _ADD_H
#define _ADD_H

int add(int a, int b)
{
    return a + b;
}

#endif
  1. Contents of 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:

  • Install the CMake Tools extension for VS Code
  • In the bottom bar, you can see the current target (in square brackets Build [mytest] and Run [mytest])you want to build / run:
  • Then just click the run button.

enter image description here


Building Google test itself

Using terminal:

  1. Go into the dir /home/user/Desktop/projects/cpp/googletest
  2. Create build/ inside it so that it looks like following:
└─cpp/googletest/
    ├─ build/
    ├─ ...other googletest files
  1. cd build
  2. Run: cmake -Dgtest_build_samples=ON -DCMAKE_BUILD_TYPE=Debug ..
  3. make -j4
  4. ./googletest/sample1_unittest

Using VS-Code

  1. Open the googletest folder into VS Code
  2. CMake extension will prompt for configuration, allow it
  3. You will see a .vscode directory. Inside it is settings.json file, open it, and add the following to it:
    "cmake.configureSettings": { "gtest_build_samples": "ON" }
  1. Build and run from the buttons in the bottom bar
Outshoot answered 15/7, 2020 at 9:24 Comment(10)
Thanks for answering but I think you misunderstood me. I dont have any project which I want to test using Google Test. I want to build and debug the Google Test library itself. Is it possible? How can I do it? PS: Please dont delete this answer, its quite good and will help me later when I use Google Test for writing tests for my project.Pahari
That should be even more simple, I will put it in the answer.Outshoot
@Outshoot When I try to Build I get fatal error: gtest/gtest.h: No such file or directory I used Using VS-Code steps.Radii
@SachithMuhandiram are you able to build using the command line? And are you trying to build GoogleTest itself or do you want to write tests for your project?Outshoot
@Outshoot Yes, I built it as your answer suggested. Now I want to integrate it for a my own project where I will write test cases. I cloned googletest and installed CUnit as VS-Code suggested way (Library)Radii
If you want to integrate it into your project, you have to follow the instructions in the first part of the post(before Building Google test itself). The second part is if you want to build googletest and maybe contribute to the project.Outshoot
@Outshoot Do I have to use abuild system (i.e: Cmake) to be able to use Google test?Lydie
Not really. You can just use a makefile or nothing at all but you will need to adjust the include paths and link the gtest library correctlyOutshoot
If anyone facing the following error : /usr/bin/ld: ../lib/libgtest.a(gtest-all.cc.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC clang: error: linker command failed with exit code 1 (use -v to see invocation), just add target_compile_options(gtest PRIVATE -fPIC) before target_link_libraries. I have solved my issue in this way. I have to link the gtest with another library.Cent
These explanations seem to imply that Google Test must be installed on each and every project where you wish to use it. Is this correct, or am I reading this wrong?Standstill

© 2022 - 2024 — McMap. All rights reserved.