首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Bazel项目中建立Catch2

如何在Bazel项目中建立Catch2
EN

Stack Overflow用户
提问于 2018-10-03 07:20:51
回答 4查看 1.1K关注 0票数 3

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

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

代码语言:javascript
复制
WORKSPACE -> empty file
src/
  Money.hpp
  Money.cpp
BUILD

在哪里构建是公正的

代码语言:javascript
复制
cc_library(
  name = "Money",
  srcs = ["Money.cpp"],
  hdrs = ["Money.hpp"]
)

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-10-03 07:20:51

经过反复学习,我成功地完成了Bazel0.16.1和Catch2 2.4.0的工作。

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

为了使用Catch2,我们需要下载catch.hpp。Catch2只是标头库,这意味着我们只需要一个文件。我把它放在test/vendor/catch2/里了。

然后,我们需要向bazel定义如何使用它。在test/vendor/catch2中,我们创建以下构建文件:

代码语言:javascript
复制
cc_library(
  name = "catch2",
  hdrs = ["catch.hpp"],
  visibility = ["//test:__pkg__"]
)

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

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

代码语言:javascript
复制
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

现在,我们用test/Money.test.cpp编写我们的测试

代码语言:javascript
复制
#include "catch.hpp"
#include "Money.hpp"

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

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

代码语言:javascript
复制
# 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如下所示:

代码语言:javascript
复制
cc_library(
    name = "Money",
    srcs = ["Money.cpp"],
    hdrs = ["Money.hpp"],
    visibility = ["//test:__pkg__"]
)

最后的文件结构如下所示:

代码语言:javascript
复制
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,您可以查看它的这里。我还把它变成了博客帖子

票数 3
EN

Stack Overflow用户

发布于 2019-11-26 12:11:36

Catch2 (v2.13.0)由Bazel提供支持。

示例

WORKSPACE.bazel

代码语言:javascript
复制
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

代码语言:javascript
复制
cc_test(
    name = "my_test",
    srcs = ["my_test.cpp"],
    defines = ["CATCH_CONFIG_MAIN"],
    deps = [
        "@catch2",
    ],
)

my_test.cpp

代码语言:javascript
复制
#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 );
}
票数 5
EN

Stack Overflow用户

发布于 2022-10-18 13:34:59

Catch2 v3 (3.1.1)

Catch2 v3被拆分为多个标头,需要C++14或更高版本,并且不再仅限于标头,这会稍微改变一些事情。

WORKSPACE.bazel:

代码语言:javascript
复制
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:

代码语言:javascript
复制
cc_test(
    name = "my_test",
    srcs = ["my_test.cpp"],
    copts = ["-std=c++14"],
    deps = [
        "@catch2//:catch2_main",
    ],
)

my_test.cpp:

代码语言:javascript
复制
#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 );
}

现在,使用

代码语言:javascript
复制
bazelisk test //:my_test --cxxopt='-std=c++14'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52621760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档