gpt4 book ai didi

c++ - 如何在 Bazel 项目中设置 Catch2

转载 作者:行者123 更新时间:2023-11-30 02:18:07 27 4
gpt4 key购买 nike

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

最佳答案

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 );
}

关于c++ - 如何在 Bazel 项目中设置 Catch2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52621760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com