CPPUnit.mk makes it easy to build unit tests

2005-11-12
2013-04-22
  • John Lakos advises in Large Scale C++ Software Design that each unit test should only include the class under direct test as well those sources which are absolutely required to get the test to link.  In particular, for reasons he eloquently explains, one should not just link in one's entire project.

    To keep things manageable, one needs a separate makefile for each unit test, so that the tests dependencies can be listed individually.  But this means that a large project will have dozens of makefiles with mostly identical content.

    My CPPUnit.mk and CPPUnit.t.mk make it easier to build unit tests using GNU make.  After declaring a few makefile variables, you say "include CPPUnit.mk" in your main makefile.  In your *.t.mk unit test makefiles, you just say:

    CPPUNIT_TESTED=ClassUnderTest ADependency AnotherDependency
    include CPPUnit.t.mk

    CPPUnit.t.mk encapsulates everything that's common to all the unit test makefiles.

    CPPUnit.mk and CPPUnit.t.mk are included in libmdc - Mike Crawford's C++ Library.  It is Free Software under the Boost Software License and is available at:

    http://www.goingware.com/libmdc/

    Complete documentation is provided in a long comment at the top of CPPUnit.mk.  The library's main Makefile is extensively commented to show you a specific example of how it is done.  There are CPPUnit tests for several of the library's classes.

    Enjoy!

    Mike

     
    • For no reason I can fathom, GNU make deletes the unit test objects (*.t.o) after it's done.  I don't think there's anything in my makefile that requests this, and I note that it uses "rm" with no "-f", whereas all the rm's in my "make clean" targets use "rm -f".

      I'm not really much of an expert at Make, so maybe one of you can figure out how I can fix it.

      It's not objectionable if you have only a few unit tests or if they were small, but in a big project it would mean a lot of extra time spent recompiling that shouldn't be necessary.