Это работает для меня:
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.
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 этот тестовый фреймворк будетбыть устаревшим, поэтому я бы не стал призывать вас вкладывать в это средства.Из примечаний к выпуску: «Шаблон проекта Managed C ++ Test больше не доступен. Вы можете продолжить использовать инфраструктуру Managed C ++ Test в ваших существующих проектах, но для новых модульных тестов рассмотрите возможность использования одной из собственных платформ тестирования, для которых предусмотрена Visual Studio.шаблоны (MSTest, Google Test) или шаблон проекта Managed C # Test "