Join us on Facebook!
— Written by Triangles on September 08, 2015 • updated on October 05, 2018 • ID 16 —
Add easy support for testing your program.
Automake is able to build and run your test suites without tearing your mind apart. The key point is to add a couple of special variables inside the glorious
Makefile.am and set up the rest properly.
Say you have a project organized as follows:
src/ test/ Makefile.am ... [many others Autotools files here] ...
src/ contains the actual source code and
test/ the test suite. I want Make to build the tests from the latter folder (possibly including the needed files from
src/) and run the suites, alerting me if anything goes wrong. Please note that I'm using non-recursive Automake.
Makefile.am and define the special variable
TESTS, which is basically the name of the executable (or script or program) that Make will run in order to do the testing. Say you have an app called Funky, I would call the test program as
funky_test. For example:
TESTS = funky_test
You can actually define multiple test programs, but I will stick to one for the sake of simplicity. Next, add
check_PROGRAMS: another variable that will define programs that are compiled when someone runs the test suite. For example:
check_PROGRAMS = funky_test
That was pretty obvious. The last step: list all the source files needed within the variable
[your_program]_SOURCES. So in our example it would be:
funky_test_SOURCES = test/main.cc src/funky-class.cc src/another-funky-class.cc
Here I'm including the main source file with the actual test suite (
test/main.cc) and other classes from the
src/ folder, needed by the test itself (
Finally: build and run your tests with
make check. Enjoy!