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

查看:76
本文介绍了如何在通过自动工具构建的项目中使用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 Test库是我的单元测试的依赖项,应该在我第一次在项目中运行"make check"时构建.这应该在某个目录中构建Google Test库.我不知道该怎么做.它提到了一些不推荐使用的自动工具脚本,我不确定他们在说什么或如何正确构建我的构建.

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 Test版本运行测试的测试?我假设我在测试目录中放了一堆Makefile.am命令.但是他们是什么?以及使用Google测试的单元测试的例子是什么?

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 Test与自动工具完美地吻合.因此,对于冗长的答案,我事先表示歉意,但是所有详细信息都应该在那儿.

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

这是怎么做的?已通过AM_PROG_AR和LT_INIT启用了Libtool. check_LTLIBRARIES意味着我们将使用libtool创建一个名为libgtest.la的便捷库.启用subdir-objects后,它将内置在/tests目录中,但未安装.这意味着,每当我们要更新测试时,就不必重新编译Google Test库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 Test库并使用它们?这些问题是相互交织的,所以我们立即回答.

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.要为您的库创建测试,您需要阅读入门.您会注意到,我们还不需要标头(尚未).我们正在链接到文件"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行来启用pthreads.最后,我们需要包括找到标题"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

现在,来自/make checkautoreconf -fiv(记下所有错误,并希望予以纠正),您应该获得运行的测试:

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天全站免登陆