如何在 Bazel 项目中设置 Catch2

How to set up Catch2 in Bazel project

我已经启动了一个简单的 C++ 项目,该项目使用 Bazel 作为构建系统,并希望将 Catch2 添加到其中作为测试框架。

这是我的项目到目前为止的样子:

WORKSPACE -> empty file
src/
  Money.hpp
  Money.cpp
BUILD

构建只是

cc_library(
  name = "Money",
  srcs = ["Money.cpp"],
  hdrs = ["Money.hpp"]
)

我希望能够为每个 cc_library 创建测试,在本例中是 Money。我尝试设置它但与 Catch2 main 混淆了。任何有关如何做到最好的建议都将不胜感激!

经过一些来回我设法让这个工作,对于 Bazel 0.16.1 和 Catch2 2.4.0。

首先让我们在 src/ 旁边创建目录 test/,以将我们的测试保存在那里。

为了使用Catch2,我们需要下载catch.hpp。 Catch2 是 header 唯一的库,这意味着我们只需要一个文件。我把它放在 test/vendor/catch2/.
然后,我们需要定义 bazel 如何使用它。在 test/vendor/catch2 中,我们创建以下 BUILD 文件:

cc_library(
  name = "catch2",
  hdrs = ["catch.hpp"],
  visibility = ["//test:__pkg__"]
)

现在 Bazel 将 Catch2 识别为一个库。我们添加了可见性属性,以便可以从 //test 包(由 /test 目录中的 BUILD 定义)中使用。

接下来,Catch2 要求我们定义一个具有正确定义的 main 方法的翻译单元。按照他们的指示,我们创建 test/main.cpp 文件:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

现在,我们在 test/Money.test.cpp:

中编写测试
#include "catch.hpp"
#include "Money.hpp"

TEST_CASE("Money works.") {
  ...
}

最后,我们需要向 Bazel 解释如何构建这一切。请注意,我们直接在文件中包含 Money.hpp 和 catch.hpp,没有相对路径,因此我们也需要记住这一点。 我们创建以下 test/BUILD 文件:

# We describe to Bazel how to build main.cpp.
# It includes "catch.hpp" directly, so we need to add
# "-Itest/vendor/catch2" compiler option.
cc_library(
    name = "catch-main",
    srcs = ["main.cpp"],
    copts = ["-Itest/vendor/catch2"],
    deps = [
        "//test/vendor/catch2"
    ]
)

# Here we define our test. It needs to build together with the catch2
# main that we defined above, so we add it to deps. We directly
# include src/Money.hpp and test/vendor/catch2/catch.hpp in
# Money.test.cpp, so we need to add their parent directories as copts.
# We also add Money and catch2 as dependencies.
cc_test(
    name = "Money",
    srcs = ["Money.test.cpp"],
    copts = ["-Itest/vendor/catch2/", "-Isrc/"],
    deps = [
        # Or "//test/vendor/catch2:catch2", it is the same.
        "//test/vendor/catch2",
        "catch-main",
        "//src:Money"
    ]
)

# Test suite that runs all the tests.
test_suite(
    name = "all-tests",
    tests = [
        "Money"
    ]
)

最后,我们只需要将 visibility 属性添加到 src/BUILD 以便可以从测试中访问它。我们修改 src/BUILD 看起来像这样:

cc_library(
    name = "Money",
    srcs = ["Money.cpp"],
    hdrs = ["Money.hpp"],
    visibility = ["//test:__pkg__"]
)

最终文件结构如下所示:

WORKSPACE
src/
  Money.hpp
  Money.cpp
  BUILD
test/
  BUILD
  main.cpp
  Money.test.cpp
  vendor/
    catch2/
      catch.hpp
      BUILD

现在您可以 运行 使用 bazel test //test:all-tests 进行测试!

我用这个例子创建了 Github 仓库,你可以查看 here。 我也把它变成了 blog post.

Bazel 开箱即用地支持 Catch2 (v2.13.0)。

例子

WORKSPACE.bazel:

workspace(name = "Catch2Demo")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "catch2",
    strip_prefix = "Catch2-2.13.0",
    urls = ["https://github.com/catchorg/Catch2/archive/v2.13.0.tar.gz"],
)

BUILD.bazel:

cc_test(
    name = "my_test",
    srcs = ["my_test.cpp"],
    defines = ["CATCH_CONFIG_MAIN"],
    deps = [
        "@catch2",
    ],
)

my_test.cpp:

#include <catch2/catch.hpp>

unsigned int Factorial( unsigned int number ) {
    return number <= 1 ? number : Factorial(number-1)*number;
}

TEST_CASE( "Factorials are computed", "[factorial]" ) {
    REQUIRE( Factorial(1) == 1 );
    REQUIRE( Factorial(2) == 2 );
    REQUIRE( Factorial(3) == 6 );
    REQUIRE( Factorial(10) == 3628800 );
}

Bazel 支持已上游到 catch2:https://github.com/catchorg/Catch2/pull/1923

使用它,您只需添加到您的 WORKSPACE:

http_archive(
  name = "com_github_catchorg_catch2",
  urls = ["https://github.com/catchorg/Catch2/archive/v2.12.1.tar.gz"],
  strip_prefix = "Catch2-2.12.1",
  sha256 = "e5635c082282ea518a8dd7ee89796c8026af8ea9068cd7402fb1615deacd91c3",
)

(如果需要更新版本,请将版本和 sha256 替换为正确的值)。

然后你的构建文件可以简单地做:

cc_library(
  name = "test_main",
  srcs = ["test_main.cpp"],
  deps = ["@com_github_catchorg_catch2//:catch2"],
)

test_main.cpp 包含:

#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"

最后,您可以像这样定义测试:

cc_library(
  name = "my_test",
  srcs = ["my_test.cpp"],
  deps = [
      ":test_main",
      "@com_github_catchorg_catch2//:catch2",
  ],
)