Writing Makefile.am to call googletest unit tests

I am trying to add my first unit test to an existing open source project. In particular, I added a new class called audio_manager:

src/audio/audio_manager.h src/audio/audio_manager.cc 

I created the src / test directory structure that reflects the structure of the implementation files, and wrote my googletest tags:

 src/test/audio/audio_manager.cc 

Now I am trying to configure my Makefile.am to compile and run the unit test:

 src/test/audio/Makefile.am 

I copied Makefile.am with:

 src/audio/Makefile.am 

Does anyone have a simple recipe for me, or is it for the cryptic documentation for the machine for me? :)

+6
c ++ unit-testing makefile automake googletest
source share
3 answers

William told me where to go. Just for the sake of the community, here is what I did:

  • I migrated my tests back to the main directory structure and added test_, as suggested by William.
  • 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" runs unit tests!

All this can be seen here: http://github.com/ksterker/adonthell/commit/aacdb0fe22f59e61ef0f5986827af180c56ae9f3

+6
source share

If the existing project already has a test structure, you should simply add:

  TESTS + = audio_manager

to existing tests / Makefile.am. If the existing project does not have a test structure, you should run a cry for the hills.

If the work for the hills is unacceptable, there is little work to create a test structure in place, but it is not insurmountable. You may prefer to do the tests with your src brother, but this is not necessary. It is probably easier to start with the new Makefile.am rather than copying Makefile.am from src, but maybe not. Perhaps all you need to do is change the lines of the form:

  bin_PROGRAMS = ...

to

  check_PROGRAMS = ...

add line

  TESTS = test-audio-manager

change the name audio_manager.cc to test-audio-manager.cc (which is not strictly necessary, but maintainability will help. I changed it to a pure personal preference) and add

  SUBDIRS = tests / audio

in src / Makefile.am. (If the SUBDIRS directive already exists, add to this assignment or use + =)

+8
source share

TESTS information in other answers, you can also specify several tests for TESTS .

No matter how many tests you specify, you actually do not need to specify them twice, instead just set TESTS to $(check_PROGRAMS) - this can help prevent an accidental situation when adding a test to check_PROGRAMS , but forgetting to add it to TESTS , resulting in your new test will be added to the assembly, but the make check will never run:

 # Unit tests check_PROGRAMS = test_audio_manager test_audio_manager_SOURCES = test_audio_manager.cc TESTS = $(check_PROGRAMS) 

... or do the same with a few 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) 
+1
source share

All Articles