简体   繁体   English

在 Visual Studio Code 中对本机 C++ 进行单元测试

[英]Unit Testing Native C++ In Visual Studio Code

I've been using Visual Studio Code to develop a low-level C++ library on a Mac.我一直在使用 Visual Studio Code 在 Mac 上开发低级 C++ 库。 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.市面上有无数的框架,但没有一个像 JUnit 那样被确立为标准。 Moreover, the Visual Studio Code Unit Test Explorer has been very scantily documented .此外,Visual Studio Code 单元测试资源管理器的文档非常少。

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.我正在使用 SCons,但如有必要,我可以切换到另一个构建系统。

For CMake.对于 CMake。 This is minimal setup for Code.这是代码的最小设置。

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

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2010的单元测试功能是否可用于本机C ++代码? - Is Visual Studio 2010's unit testing feature usable for native C++ code? Visual Studio是否有扩展可以优化C ++代码单元测试? - Is there any extension for visual studio to optimize C++ code unit testing? 单元测试本地C ++代码 - Unit testing native C++ code 使用带有字符串而不是宽字符串的 Visual Studio 原生 C++ 单元测试框架 - Using Visual Studio native C++ unit testing framework with strings instead of wide strings 如何使用CppUnitTestFramework在Visual Studio本机C ++单元测试中获取$(ProjectDir)路径? - How to get the $(ProjectDir) path in a Visual Studio native C++ Unit Testing using CppUnitTestFramework? 在Visual Studio 2012中单元测试exe中的C ++代码时阻力最小的路径 - Path of least resistance when unit testing C++ code in an exe, in Visual Studio 2012 需要Visual Studio 2005兼容的C ++单元测试框架 - Need Visual Studio 2005 Compatible C++ Unit Testing Framework 在Visual Studio 2012中对C ++控制台应用程序进行单元测试 - Unit testing c++ console application in Visual Studio 2012 使用Visual Studio测试框架进行C ++单元测试 - C++ unit testing with Visual Studio test framework 使用Visual Studio对本机VS C ++的gtest单元测试执行代码覆盖 - Using Visual Studio to do Code Coverage on gtest unit tests for native VS C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM