【问题标题】:Unit Testing Native C++ In Visual Studio Code在 Visual Studio Code 中对本机 C++ 进行单元测试
【发布时间】:2018-04-12 15:21:48
【问题描述】:

我一直在使用 Visual Studio Code 在 Mac 上开发低级 C++ 库。它似乎非常优雅地与编译器工具链集成。但至于单元测试,我完全迷失了。有一个myriad of frameworks,它们都没有像 JUnit 那样被建立为标准。而且,Visual Studio Code Unit Test Explorer 已经很scantily documented了。

哪些单元测试框架与代码前端集成得很好?应该如何建立一个单元测试项目?我正在使用 SCons,但如有必要,我可以切换到另一个构建系统。

【问题讨论】:

  • 如果您使用的是 CMake,那么 Boost.Tests 与 CMake 配合得很好。在构建调试构建时,我总是设置测试以构建和自动运行。
  • 谢谢。你碰巧知道什么对 SCons 有效吗?另外,您是否可以使用代码前端来运行和监控您的测试?
  • 不幸的是,我从未使用过 SCons。我不确定代码前端,我只是更喜欢控制台来完成此类任务,但我几乎可以肯定,可以通过按 F5 或其他任何东西来设置要构建的代码,并且内置控制台会弹出编译然后测试输出.

标签: c++ unit-testing visual-studio-code


【解决方案1】:

对于 CMake。这是 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
        }
    ]
}

现在我可以SHIFT + CTRL + b 构建我的项目,CMake 将在内置代码控制台中编译输出结束时向我显示 Boost.Tests(通过/失败)。

代码控制台输出示例:

> 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 2018-10-21
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多