在Visual C++ 2015中使用Catch2

Using Catch2 in Visual C++ 2015

本文关键字:Catch2 2015 Visual C++      更新时间:2023-10-16

我正在尝试使用 Catch 框架制作一个单元测试项目,但遇到了链接错误。

我已经设置了如下项目:

  1. 创建本机单元测试项目
  2. 将 Catch 添加到包含目录
  3. #include <catch.hpp>添加到 stdafx.h
  4. 编写以下简单源文件

单元测试.cpp:

#include "stdafx.h"
namespace Catch2_Test
{
  TEST_CASE("Y U no work")
  {
    REQUIRE(1);
  }
}

用于将 Catch 集成到 Visual Studio
中请参考 ACCU 文章 将捕获测试框架集成到 Visual Studio 中,作者是 Malcolm Noyes

为了在预编译的标头中使用 Catch2
参考 Catch2 问题 1061,其中 horenmar 举了一个例子。这些更改已作为 v2.1.0 的一部分发布

总之,给出的解决方案是:

// stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#define CATCH_CONFIG_ALL_PARTS
#include "catch.hpp"
// PCH-test.cpp:
#include "stdafx.h"
#undef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED
#define CATCH_CONFIG_IMPL_ONLY
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
// tests1.cpp:
#include "stdafx.h"
TEST_CASE("FooBarBaz") {
    REQUIRE(1 == 2);
}

问题是我从主分支克隆了 Catch2,而 VS 集成在 Catch 之外的一个分支上工作。