12

I've been using Visual Studio Code to develop a low-level C++ library on a Mac. It seems to integrate with the compiler toolchain quite gracefully. But as to Unit Testing, I'm totally lost. There are a myriad of frameworks out there, none of them having been established as a standard, the way JUnit has. Moreover, the Visual Studio Code Unit Test Explorer has been very scantily documented.

Which unit testing frameworks integrate well with the Code frontend? How should one set up a unit testing project? I'm using SCons but I can switch to another build system if necessary.

3
  • 1
    If you are using CMake then Boost.Tests works great with CMake. I'm always setting up tests to build and auto run when building debug build. Commented Oct 31, 2017 at 12:57
  • Thanks. Do you happen to know what works well with SCons? Also, do you get to use the Code frontend to run and monitor your tests? Commented Oct 31, 2017 at 13:03
  • Unfortunately I have never used SCons. I'm not sure about Code frontend, I just prefer console for such tasks, but I'm almost certain that it is possible to setup Code to build by pressing F5 or whatever and built-in console pops up with compilation and then tests output. Commented Oct 31, 2017 at 13:08

1 Answer 1

3

For CMake. This is minimal setup for Code.

CMakeLists.txt

cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CXX_FLAGS_RELEASE "-O2")

add_library(example ./some-path/example.cpp)

project(tests)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework)

add_executable(${PROJECT_NAME} ./tests/tests.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} example ${Boost_LIBRARIES})

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${PROJECT_NAME})

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "type": "shell",
            "command": "cd build && make" // cd to your build directory
        }
    ]
}

Now I can SHIFT + CTRL + b to build my project and CMake will show me Boost.Tests (passed/failed) at the end of compiling output in built-in Code console.

Example output of Code's console:

> Executing task: cd build && make <

[ 75%] Built target example
Scanning dependencies of target tests
[ 87%] Building CXX object CMakeFiles/tests.dir/tests/tests.cpp.o
[100%] Linking CXX executable tests
Running 74 test cases...

*** No errors detected
[100%] Built target tests

Terminal will be reused by tasks, press any key to close it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.