Creating Tests for “make check” with GNU Autotools

Creating Tests for "make check" with GNU Autotools

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When working with GNU Autotools for your project’s build system, you might want to integrate automated tests that run seamlessly with the command make check. This guide will walk you through the steps required to set up such tests, ensuring they are part of your build process without necessitating additional dependencies for your users.

Setting Up Your Tests

To ensure your tests run when you issue make check, you need to add them to the TESTS variable. Here’s how you can do it:

Basic Setup

Assuming you have already built the executable that runs the unit tests, you can simply add the name of the executable to the TESTS variable:

TESTS=my-test-executable

This setup will automatically run your test executable when you use make check. If the executable returns a non-zero value, it will be reported as a test failure.

Multiple Test Executables

If you have multiple unit test executables, list them all in the TESTS variable:

TESTS=my-first-test my-second-test my-third-test

All listed executables will be run when you use make check.

Advanced Configuration

In some cases, you may want to integrate more advanced testing frameworks or ensure that your test code is not bundled with distribution packages. Here are a few additional tips:

Using the Check Framework

For those using the Check framework, you can set up your tests in a structured manner:

PKG_CHECK_MODULES([CHECK], [check >= 0.9.10])
TESTS = check_foo
check_PROGRAMS = check_foo
check_foo_SOURCES = check_foo.c $(top_builddir)/src/foo.h
check_foo_CFLAGS = @CHECK_CFLAGS@

Write your test code in tests/check_foo.c and ensure it includes the relevant assertions:

START_TEST (test_foo)
{
    ck_assert( foo() == 0 );
    ck_assert_int_eq( foo(), 0);
}
END_TEST

Dependency-Free Testing

If you prefer a method without dependencies, you can configure your tests as follows:

# src/Makefile.am
check_PROGRAMS = test1 test2
test1_SOURCES = test/test1.c code_needed_to_test1.h code_needed_to_test1.c
test2_SOURCES = test/test2.c code_needed_to_test2.h code_needed_to_test2.c
TESTS = $(check_PROGRAMS)

With this setup, make check will work naturally and provide formatted, summarized output. Additionally, when you use make dist, the test code will not be included in the distribution tarball, keeping your package clean.

Conclusion

By following the steps above, you can efficiently integrate automated tests into your GNU Autotools build process. This ensures that your project’s integrity is maintained through continuous testing, without imposing additional dependencies on your users.

Enhance Your Testing with Repeato

For projects involving mobile applications, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests quickly and efficiently using computer vision and AI. With its intuitive test recorder and scripting interface, Repeato can handle complex use cases and streamline your testing process, just as you have done with GNU Autotools for your C++ projects.

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