在 Visual C++ 2015 中使用 Catch2

Using Catch2 in Visual C++ 2015

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

我将项目设置如下:

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

unittest.cpp:

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

问题是我从 master branch, while the VS integration worked on a branch off Catch.

克隆了 Catch2

用于将 Catch 集成到 Visual Studio
参考 ACCU 文章 Integrating the Catch Test Framework into Visual Studio, by Malcolm Noyes

在预编译头文件中使用 Catch2,
参考 Catch2 issue 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);
}