我已经启动了一个简单的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混淆了。任何有关如何做到最好的建议都将受到赞赏!
答案 0 :(得分:2)
经过一番来回的尝试后,我设法将其用于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
包(由/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
运行测试了!
答案 1 :(得分:1)
挡板对catch2的支持:https://github.com/evanmoran/catch2-bazel
示例
WORKSPACE.bazel :
workspace(name = "Catch2Demo")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "catch2",
url = "https://github.com/evanmoran/catch2-bazel/archive/v2.2.3.tar.gz",
strip_prefix = "catch2-bazel-2.2.3",
)
BUILD.bazel :
cc_test(
name = "my_test",
srcs = ["my_test.cpp"],
deps = ["@catch2//:main"],
)
my_test.cpp :
#include "catch2.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 );
}
答案 2 :(得分:0)
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",
],
)