1

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

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

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

BUILD 就在哪里

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

我希望能够为每个创建测试cc_library,在这种情况下为Money. 我尝试设置它,但对 Catch2 main 感到困惑。任何关于如何做到最好的建议都值得赞赏!

4

3 回答 3

3

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

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

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

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

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

接下来,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 repo,你可以在这里查看。我还把它变成了一篇博文

于 2018-10-03T07:20:51.497 回答
3

开箱即用的 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"],
)

构建.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 );
}
于 2019-11-26T12:11:36.420 回答
0

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

使用它,您只需要添加到您的工作区:

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",
  ],
)
于 2020-05-18T14:51:06.677 回答