Reputation: 26140
I am trying to add my first unit test to an existing Open Source project. Specifically, I added a new class, called audio_manager:
src/audio/audio_manager.h
src/audio/audio_manager.cc
I created a src/test directory structure that mirrors the structure of the implementation files, and wrote my googletest unit tests:
src/test/audio/audio_manager.cc
Now, I am trying to set up my Makefile.am to compile and run the unit test:
src/test/audio/Makefile.am
I copied Makefile.am from:
src/audio/Makefile.am
Does anyone have a simple recipe for me, or is it to the cryptic automake documentation for me? :)
Upvotes: 6
Views: 4897
Reputation: 3388
Complimenting the information in the other answers, you can also specify multiple tests to TESTS
.
Regardless of how many tests you specify, you don't actually have to specify them twice, instead just set TESTS
to $(check_PROGRAMS)
- this can help to prevent an accidental situation of adding your test to check_PROGRAMS
but forgetting to add it to TESTS
, causing your new test to be added to the build, but never being run by make check
:
# Unit tests
check_PROGRAMS = test_audio_manager
test_audio_manager_SOURCES = test_audio_manager.cc
TESTS = $(check_PROGRAMS)
...or to do the same with multiple tests:
# Unit tests
check_PROGRAMS = test_audio_manager test_video_manager
test_audio_manager_SOURCES = test_audio_manager.cc
test_video_manager_SOURCES = test_video_manager.cc
TESTS = $(check_PROGRAMS)
Upvotes: 2
Reputation: 26140
William's answer got me where I needed to go. Just for the sake of the community, here's what I ended up doing:
I added a few lines to src/audio/Makefile.am to enable unit tests:
# Unit tests
noinst_PROGRAMS = test_audio_manager
test_audio_manager_SOURCES = $(libadonthell_audio_la_SOURCES) test_audio_manager.cc
test_audio_manager_CXXFLAGS = $(libadonthell_audio_la_CXXFLAGS)
test_audio_manager_LDADD = $(libadonthell_audio_la_LIBADD) -lgtest
TESTS = test_audio_manager
Now, running "make check" fires the unit tests!
All of this can be seen here: http://github.com/ksterker/adonthell/commit/aacdb0fe22f59e61ef0f5986827af180c56ae9f3
Upvotes: 7
Reputation: 212238
If the existing project already has a test structure in place, then you should just add:
TESTS += audio_manager
to the existing tests/Makefile.am. If the existing project does not have a test structure in place, you should run screaming for the hills.
If running for the hills is not acceptable, there's a fair bit of work in getting the test structure in place, but it's not insurmountable. You might prefer to make tests a sibling of src, but that's not necessary. It's probably easier to start with a fresh Makefile.am rather than copying the Makefile.am from src, but maybe not. Possibly, all you'll need to do is change lines of the form:
bin_PROGRAMS = ...
to
check_PROGRAMS = ...
add the line
TESTS = test-audio-manager
change the name of audio_manager.cc to test-audio-manager.cc (that's not strictly necessary, but will help maintainability. I changed _ to - purely out of personal preference) and add a
SUBDIRS = tests/audio
to src/Makefile.am. (If there's already a SUBDIRS directive, append to that assignment or use +=)
Upvotes: 9