c++visual-studio-codecmakegoogletest

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


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

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?


Solution

    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
    
    set(GOOGLETEST_VERSION 1.15.2)
    
    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:

    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