CMake用于集成的Microsoft单元测试框架(VS2017)

编程入门 行业动态 更新时间:2024-10-24 16:24:42
本文介绍了CMake用于集成的Microsoft单元测试框架(VS2017)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

类似于我之前的问题,但专门针对Microsoft单元测试框架:

Similar to my previous question but specifically for the Microsoft Unit Testing Framework:

Visual Studio 2017已集成C ++单元测试(MS单元测试,google测试等).我如何创建CMakeLists.txt文件,该文件将创建一个使用集成IDE测试(特别是使用Microsoft单元测试框架)的项目,例如此项目?

Visual Studio 2017 has integrated C++ unit testing (MS unit testing, google test, etc.). How can I create a CMakeLists.txt file that will create a project like this that will use the integrated IDE testing, specifically using the Microsoft Unit Testing Framework?

谢谢!

推荐答案

这对我有用:

CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 2.8) project(tests) include_directories("D:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Auxiliary/VS/UnitTest/include") link_directories("D:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Auxiliary/VS/UnitTest/lib/x86/Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib") add_library(tests SHARED tests.cpp)

您可能需要将路径更改为安装Visual Studio的路径.

You might need to change the path to where you have Visual Studio installed.

tests.cpp

tests.cpp

#include <CppUnitTest.h> using namespace Microsoft::VisualStudio::CppUnitTestFramework; TEST_MODULE_INITIALIZE(ModuleInitialize) { Logger::WriteMessage("In Module Initialize"); } TEST_MODULE_CLEANUP(ModuleCleanup) { Logger::WriteMessage("In Module Cleanup"); } TEST_CLASS(Class1) { public: Class1() { Logger::WriteMessage("In Class1"); } ~Class1() { Logger::WriteMessage("In ~Class1"); } TEST_CLASS_INITIALIZE(ClassInitialize) { Logger::WriteMessage("In Class Initialize"); } TEST_CLASS_CLEANUP(ClassCleanup) { Logger::WriteMessage("In Class Cleanup"); } TEST_METHOD(Method1) { Logger::WriteMessage("In Method1"); Assert::AreEqual(0, 0); } TEST_METHOD(Method2) { Assert::Fail(L"Fail"); } };

在测试"输出中,我有

[1/4/2019 9:51:56 PM Informational] ------ Discover test started ------ [1/4/2019 9:51:59 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:51:59 PM Informational] ========== Discover test finished: 0 found (0:00:02.8603805) ========== [1/4/2019 9:54:14 PM Informational] ------ Discover test started ------ [1/4/2019 9:54:18 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:54:18 PM Informational] ========== Discover test finished: 0 found (0:00:03.7709729) ========== [1/4/2019 9:54:38 PM Informational] ------ Discover test started ------ [1/4/2019 9:54:39 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:54:39 PM Informational] ========== Discover test finished: 0 found (0:00:00.7098537) ========== [1/4/2019 9:54:49 PM Informational] ------ Discover test started ------ [1/4/2019 9:54:50 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:54:50 PM Informational] ========== Discover test finished: 0 found (0:00:00.7292453) ========== [1/4/2019 9:54:56 PM Informational] ------ Discover test started ------ [1/4/2019 9:54:56 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:54:56 PM Informational] ========== Discover test finished: 0 found (0:00:00.7365023) ========== [1/4/2019 9:55:00 PM Informational] ------ Discover test started ------ [1/4/2019 9:55:01 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:55:01 PM Informational] ========== Discover test finished: 0 found (0:00:00.7208954) ========== [1/4/2019 9:55:01 PM Informational] ------ Discover test started ------ [1/4/2019 9:55:02 PM Warning] No test is available in D:\dev\cpptest\SO54039205\Debug\SO54039205.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. [1/4/2019 9:55:02 PM Informational] ========== Discover test finished: 0 found (0:00:00.6999031) ========== [1/4/2019 10:34:09 PM Informational] ------ Discover test started ------ [1/4/2019 10:34:13 PM Informational] ========== Discover test finished: 2 found (0:00:04.0145243) ========== [1/4/2019 10:34:19 PM Informational] ------ Run test started ------ [1/4/2019 10:34:20 PM Informational] In Module Initialize [1/4/2019 10:34:20 PM Informational] In Class Initialize [1/4/2019 10:34:20 PM Informational] In Class1 [1/4/2019 10:34:20 PM Informational] In Method1 [1/4/2019 10:34:20 PM Informational] In ~Class1 [1/4/2019 10:34:20 PM Informational] In Class1 [1/4/2019 10:34:20 PM Informational] In ~Class1 [1/4/2019 10:34:20 PM Informational] In Class Cleanup [1/4/2019 10:34:20 PM Informational] In Module Cleanup [1/4/2019 10:34:21 PM Informational] ========== Run test finished: 2 run (0:00:01.1969982) ==========

请注意,自Visual Studio 2019起,该测试框架将被弃用,因此我不建议您对其进行投资.从发行说明中: 托管C ++测试项目模板不再可用.您可以在现有项目中继续使用托管C ++测试框架,但对于新的单元测试,请考虑使用Visual Studio提供模板的本机测试框架之一(MSTest,Google测试)或托管的C#测试项目模板"

Please note that from Visual Studio 2019 this test framework will be deprecated, so I would not adive you to invest in it. From the release notes: "The Managed C++ Test Project template is no longer available. You can continue using the Managed C++ Test framework in your existing projects but, for new unit tests, consider using one of the native test frameworks for which Visual Studio provides templates (MSTest, Google Test) or the Managed C# Test Project template"

更多推荐

CMake用于集成的Microsoft单元测试框架(VS2017)

本文发布于:2023-11-13 23:57:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   单元测试   CMake   Microsoft

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!