如何在通过自动工具构建的项目中使用 Google Test? [英] How can I use Google Test with my project that builds via autotools?

查看:26
本文介绍了如何在通过自动工具构建的项目中使用 Google Test?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有一些答案有点,有点道理,但我不知道如何执行.而且我还没有找到一个全面的答案.

It seems like there are a few answers that kind-of, sort-of make sense, but that I don't know how to carry out. And I haven't found a comprehensive answer.

Google Test 不应是已安装的库,而应与项目一起构建.(请参阅 常见问题解答.)可以看出,这意味着 Google 测试库是我的单元测试的依赖项,应该在我第一次在我的项目中运行make check"时构建.这应该在某个目录中构建 Google 测试库.我不知道该怎么做.它提到了一些已弃用的 autotools 脚本,我不确定他们在说什么或如何正确地指出我的构建.

Google Test should not be an installed library, it should be built with the project. (See the FAQ.) As far as I can tell, this means the Google Test libraries are a dependency of my unit tests, and should be built when I run "make check" within my project for the first time. This should build Google Test libraries in some directory. I don't know how to do this. It mentions some autotools script that's deprecated, and I'm not sure what they're talking about or how to point my build at it properly.

假设构建成功,我该如何编写一个使用本地编译版本的 Google 测试来运行测试的测试?我假设有一堆 Makefile.am 命令放在我的测试目录中.但它们是什么?什么是使用 Google Test 的单元测试示例?

Assuming the build is successful, how do I write a test that uses my locally-compiled version of Google Test to run tests? I assume that there are a bunch of Makefile.am commands I put in my tests directory. But what are they? And what's an example of a unit test that uses Google Test?

推荐答案

我已经满意地解决了这个问题!我现在将完全继续前进.这基本上是要求一个教程.有很多必须做出的决定,希望是合乎逻辑的,以便 Google 测试与自动工具很好地吻合.所以我提前为冗长的答案道歉,但所有细节都应该在那里.

I have solved the problem to my satisfaction! I will move on entirely now. This is basically asking for a tutorial. There are a lot of decisions that must be made, hopefully logically, so that Google Test dovetails nicely into autotools. So I apologize in advance for the long answer, but all the details should be there.

为了理解答案,需要稍微改写问题.我们正在将 Google Test 编译为我们的测试代码将链接到的库.将不会安装该库.我们要问的问题是

In order to understand the answer, the question needs to be rephrased a little. We are compiling Google Test as a library which our test code will link to. The library will not be installed. The question we want to ask is

"我们如何配置自动工具以将 Google Test 编译为库我们的测试代码可以链接到哪个?"

"How do we configure autotools to compile Google Test as a library which our test code can link against?"

为此,我们需要下载 Google Test 并将其放入我们的项目中.我使用 Github,所以我通过在我的项目的根路径中添加一个子模块来做到这一点:

In order to do that, we need to download Google Test and place it into our project. I use Github, so I do that by adding a submodule in the root path of my project:

$ git submodule add git@github.com:google/googletest.git
$ git submodule init
$ git submodule update

这会将 googletest 下载到我的项目的根目录中:

This downloads googletest into my root of my project:

/:
    Makefile.am
    configure.ac
    src/:
        (files for my project)
    tests/:
        (test files)
    googletest/:
        googletest/:
            include/:
                (headers, etc., to be included)
                gtest/:
                    gtest.h
            m4/:
                (directory for m4 scripts and things)
            src/:
                (source files for Google Test)

我需要按照说明进行编译.我只希望在运行 make check 时构建 Google 测试库,所以我将使用 check_LTLIBRARIES.我在/tests 中的测试 Makefile.am 中添加以下内容:

I need to compile per the instructions. I only want the Google Test library to be built upon running make check, so I will use check_LTLIBRARIES. I add the following to my tests Makefile.am in /tests:

check_LTLIBRARIES = libgtest.la
libgtest_la_SOURCES = ../googletest/googletest/src/gtest-all.cc
libgtest_la_CPPFLAGS = -I$(top_srcdir)/googletest/googletest/include -I$(top_srcdir)/googletest/googletest
libgtest_la_LDFLAGS = -pthread

这需要在 configure.ac 中启用子目录对象.这是通过将其添加到 AM_INIT_AUTOMAKE 行来完成的.我还需要在 AC_CONFIG_FILES 中包含 makefile.我们还想使用 libtool,因为我们正在编译库文件(我稍后会解释为什么以及如何工作).要使用 libtool,我们添加 AM_PROG_AR、LT_INIT.我们希望 autoreconf 将 m4 宏安装到/m4,然后我们希望 automake 找到它们,所以我们需要 AC_CONFIG_MACRO_DIRS.我的 configure.ac 更新了行:

This requires subdir-objects to be enabled in configure.ac. That is accomplished by adding it to the AM_INIT_AUTOMAKE line. I also need to include the makefile in AC_CONFIG_FILES. We also want to use libtool, because we are compiling library files (I'll explain why and how that works in a moment). To use libtool, we add AM_PROG_AR, LT_INIT. We want autoreconf to install m4 macros to /m4, and then we want automake to find them, so we need AC_CONFIG_MACRO_DIRS. My configure.ac has lines updated:

AM_INIT_AUTOMAKE([-Wall -Werror subdir-objects])
...
AM_PROG_AR
LT_INIT
AC_CONFIG_MACRO_DIRS([m4])
...
AC_CONFIG_FILES([Makefile
                 src/Makefile
                 tests/Makefile
                 ])

我还需要在/Makefile.am 的/m4 宏目录中包含子目录和指向宏的行:

I also need to include the subdirectory and a line pointing to the macros in the /m4 macros directory in my /Makefile.am:

ACLOCAL_AMFLAGS = -I m4

SUBDIRS = src tests

这做了什么? Libtool 已启用 AM_PROG_AR 和 LT_INIT.check_LTLIBRARIES 意味着我们将使用 libtool 创建一个名为 libgtest.la 的便利库.启用 subdir-objects 后,它将被构建到/tests 目录中,但不会安装.这意味着,无论何时我们想要更新我们的测试,我们都不必重新编译 Google 测试库 libgtest.la.这将节省测试时间并帮助我们更快地迭代.然后,我们将希望稍后在更新它们时针对它编译我们的单元测试.该库将仅在运行 make check 时编译,如果我们只想 makemake install 则不编译它可以节省时间.

What has this done? Libtool has been enabled with AM_PROG_AR and LT_INIT. The check_LTLIBRARIES means we will use libtool to create what's called a convenience library called libgtest.la. With subdir-objects enabled, it will be built into the /tests directory, but not installed. This means that, whenever we want to update our tests, we don't have to recompile the Google Test library libgtest.la. This will save time when testing and help us iterate faster. Then, we will want to compile our unit tests against it later as we update them. The library will only be compiled upon running make check, saving time by not compiling it if all we want to do is make or make install.

现在,需要改进第二个问题:您如何 (a) 创建一个测试 (b) 链接到 Google 测试库并因此使用它们?这些问题有点交织在一起,所以我们马上回答.

Now, the second problem needs to be refined: How do you (a) create a test (b) that is linked to the Google Test libraries and thus uses them? The questions are kind of intertwined, so we answer them at once.

创建测试只需将以下代码放入位于 /tests/gtest.cppgtest.cpp 文件中:

Creating a test is just a matter of putting the following code into a gtest.cpp file located at /tests/gtest.cpp:

#include "gtest/gtest.h" // we will add the path to C preprocessor later

TEST(CategoryTest, SpecificTest)
{
    ASSERT_EQ(0, 0);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}

这仅运行简单的测试 0=0.要为您的库创建测试,您需要阅读 primer.您会注意到我们(还)不需要标题.我们正在链接到文件gtest/gtest.h",因此我们需要确保我们告诉 automake 包含一个具有 gtest/gtest.h 的目录.

This runs only the simple test 0=0. To create a test for your library, you need to read the primer. You'll notice we don't need a header for this (yet). We are linking to the file "gtest/gtest.h", so we'll need to make sure that we tell automake to include a directory that has gtest/gtest.h.

接下来,我们需要告诉 automake 我们要构建一个测试并运行它.该测试将构建到我们不想安装的可执行文件中.然后 automake 将运行该可执行文件.它将报告该可执行文件是否表明测试通过或失败.

Next, we need to tell automake that we want to build a test and run it. The test is going to build into an executable that we don't want to install. Then automake is going to run that executable. It will report whether that executable says the tests passed or failed.

Automake 通过在 makefile 中查找变量 check_PROGRAMS 来做到这一点.这些是它将编译的程序,但不一定会运行它们.所以我们添加到 /tests/Makefile.am:

Automake does that by looking in the makefile for the variable check_PROGRAMS. These are the programs it will compile, but it won't necessarily run them. So we add to /tests/Makefile.am:

check_PROGRAMS = gtest

gtest_SOURCES = gtest.cpp

gtest_LDADD = libgtest.la

gtest_LDFLAGS = -pthread

gtest_CPPFLAGS = -I$(top_srcdir)/googletest/googletest/include -I$(top_srcdir)/googletest/googletest -pthread

gtest_SOURCES 找到 /tests/gtest.cpp 文件并编译它.gtest_LDADD 链接到 libgtest.la,它将被编译到/tests 目录中.Google 希望我们使用 gtest_LDFLAGS 行来启用 pthread.最后,我们需要包含头文件gtest/gtest.h"所在的位置.将被找到,这就是 gtest_CPPFLAGS 行.Google 还希望我们包含 /googletest/googletest 位置,并包含

The gtest_SOURCES finds the /tests/gtest.cpp file and compiles it. gtest_LDADD links against libgtest.la which will be compiled into the /tests directory. Google wants us to use the gtest_LDFLAGS line to enable pthreads. Finally, we need to include the location where the header "gtest/gtest.h" will be found, and that is the gtest_CPPFLAGS line. Google also wants us to include the /googletest/googletest location, and include the

现状: Google 测试库 libgtest.la 将使用 make 编译到目录/tests 中,但不会被安装.二进制 gtest 只会用 make check 编译,但不会被安装.

The state of things: The Google Test library libgtest.la will compile with make into the directory /tests, but not be installed. The binary gtest will only be compiled with make check, but will not be installed.

接下来我们要告诉 automake 实际运行编译后的二进制 gtest 并报告错误.这是通过在 /tests/Makefile.am 中添加一行来完成的:

Next we want to tell automake to actually run the compiled binary gtest and report errors. This is accomplished by adding a line to /tests/Makefile.am:

TESTS = gtest

最终的/tests/Makefile.am 如下所示:

The final /tests/Makefile.am looks like this:

check_LTLIBRARIES = libgtest.la
libgtest_la_SOURCES = ../googletest/googletest/src/gtest-all.cc
libgtest_la_CPPFLAGS = -I$(top_srcdir)/googletest/googletest/include -I$(top_srcdir)/googletest/googletest -pthread

check_PROGRAMS = gtest demo

gtest_SOURCES = gtest.cpp ../src/fields.cpp

gtest_LDADD = libgtest.la

gtest_LDFLAGS = -pthread

gtest_CPPFLAGS = -I$(top_srcdir)/googletest/googletest/include -I$(top_srcdir)/src

demo_SOURCES = demo.cpp ../src/fields.cpp

demo_CPPFLAGS = -I$(top_srcdir)/src

TESTS = gtest

现在,来自 /autoreconf -fiv(注意任何错误并希望修复它们)和 make check,你应该得到一个测试运行:

Now, autoreconf -fiv (note any errors and hopefully fix them) from /, and make check and you should get a test that runs:

build(dev)$ make check
Making check in tests
/Applications/Xcode.app/Contents/Developer/usr/bin/make  gtest
make[2]: `gtest' is up to date.
/Applications/Xcode.app/Contents/Developer/usr/bin/make  check-TESTS
PASS: gtest
============================================================================
Testsuite summary for IonMotion 0.0.1
============================================================================
# TOTAL: 1
# PASS:  1
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
============================================================================

这篇关于如何在通过自动工具构建的项目中使用 Google Test?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆