[CMake] Unit Testing

Philip Lowman philip at yhbt.com
Mon Jan 5 03:36:20 EST 2009


On Mon, Jan 5, 2009 at 12:32 AM, Jeffrey Drake <jeffd at techsociety.ca> wrote:

>
> I have been trying to figure out the easiest way to setup unit tests. I
> have seen a unit testing framework called 'check' (some documentation:
>
> http://check.sourceforge.net/doc/check.html/Setting-Up-the-Money-Build.html#Setting-Up-the-Money-Build) but the problem seems to be that its examples are all with automake.
>
> So what is the easiest way of doing automated unit tests with CMake? I
> have looked at CTest, but I am not understanding it very well either. I
> would need a very basic example to see how it works.


First you need to pick a unit testing framework for C++ you like.  There are
plenty to choose from.  Once you have picked one out setting it up to work
with CTest is usually fairly trivial unless you're choosing one that needs
to do some autocoding in which case it's a tad more complicated.

How you break your tests down is up to you.  Ultimately there will be a
main() somewhere that calls the test assertions you want for a particular
test.  This main() source file along with any dependents needs to be
compiled by CMake and linked just like any other binary.  You may also need
to link your test against libraries.

ADD_EXECUTABLE(FooTest FooTest.cc)
TARGET_LINK_LIBRARIES(FooTest bar)

To add it as a test to CTest just do:

ENABLE_TESTING() # once
ADD_TEST(Foo FooTest) # for all test executables

-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090105/edb51ce2/attachment.htm>


More information about the CMake mailing list