如何在 Bazel 项目中设置 Catch2

How to set up Catch2 in Bazel project

本文关键字:设置 Catch2 项目 Bazel      更新时间:2023-10-16

我已经启动了一个简单的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 混淆了。任何关于如何做到最好的建议,不胜感激!

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

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 0.16.1 和 Catch2 2.4.0。

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

为了使用 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存储库,你可以在这里查看。 我也把它变成了一篇博客文章。

Catch2 v3 (3.1.1(

Catch2 v3 分为多个标头,需要 C++14 或更高,并且不再是仅标头,这略有改变

WORKSPACE.bazel:
workspace(name = "Catch2Demo")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "catch2",
strip_prefix = "Catch2-3.1.1",
urls = ["https://github.com/catchorg/Catch2/archive/v3.1.1.tar.gz"],
)
BUILD.bazel:
cc_test(
name = "my_test",
srcs = ["my_test.cpp"],
copts = ["-std=c++14"],
deps = [
"@catch2//:catch2_main",
],
)
my_test.cpp:
#include <catch2/catch_test_macros.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 );
}

现在,运行测试

bazelisk test //:my_test --cxxopt='-std=c++14'

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",
],
)