Integrating Google Test with Autotools: A Comprehensive Guide

Integrating Google Test with Autotools: A Comprehensive Guide

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Integrating Google Test into a project that uses Autotools can be a challenging task. This guide provides a step-by-step approach to seamlessly compile and run your tests using Google Test within an Autotools environment.

The First Problem: Compiling Google Test as a Library

To compile Google Test as a library that your test code can link against, follow these steps:

  1. Download Google Test and place it into your project. Using Git, add Google Test as a submodule:
    $ git submodule add [email protected]:google/googletest.git
    $ git submodule init
    $ git submodule update
  2. Update your project structure to include the Google Test directories:
    /:
        Makefile.am
        configure.ac
        src/: (files for your project)
        tests/: (test files)
        googletest/:
            googletest/:
                include/: (headers, etc.)
                src/: (source files for Google Test)
                m4/: (m4 scripts)
  3. Modify your tests/Makefile.am to compile the Google Test library only when running make check:
    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
  4. Enable subdir-objects and other necessary configurations in configure.ac:
    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
                     ])
  5. Include the macro directory and subdirectories in Makefile.am:
    ACLOCAL_AMFLAGS = -I m4
    
    SUBDIRS = src tests

The Second Problem: Creating and Running Tests

Once Google Test is compiled, the next step is to create and run tests that link to the Google Test libraries. Follow these instructions:

  1. Create a test file, for example, gtest.cpp in your tests directory:
    #include "gtest/gtest.h"
    
    TEST(CategoryTest, SpecificTest) {
        ASSERT_EQ(0, 0);
    }
    
    int main(int argc, char **argv) {
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
  2. Update tests/Makefile.am to compile and run the test:
    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
    
    TESTS = gtest

Run autoreconf -fiv from your project root, then use make check to compile and run your tests.

Using Repeato for Automated Testing

If you are looking for a more streamlined and efficient way to create, run, and maintain your automated tests, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that leverages computer vision and AI to simplify the testing process. It offers an intuitive test recorder for beginners and a scripting interface for advanced users, making it versatile for various testing needs. Repeato also allows testing websites inside an Android emulator or device, with explicit web testing support coming soon.

Like this article? there’s more where that came from!