[CMake] Multiple tests in a single file

David Cole david.cole at kitware.com
Sat Jan 7 09:41:23 EST 2012


On Fri, Jan 6, 2012 at 6:47 PM, David Doria <daviddoria at gmail.com> wrote:
> On Fri, Jan 6, 2012 at 5:54 PM, Jean-Christophe Fillion-Robin
> <jchris.fillionr at kitware.com> wrote:
>> Hi David,
>>
>> Not too long ago I was browsing the project of a friend who worked on BTK
>> (The toolkit used by Mokka, a motion kinematic & kinetic analyser) [1].
>>
>> I noticed that he is using cxxtest [2] along with ctest. It seems it could
>> to address the use case you are describing.
>>
>> See Example integration [3] and cxxtest source [4]
>>
>> Hth
>> Jc
>>
>> [1] https://b-tk.googlecode.com/svn/web/mokka/index.html
>> [2] http://cxxtest.tigris.org/
>> [3]
>> https://code.google.com/p/b-tk/source/browse/BTK/trunk/Testing/Code/C3DFileReaderTest.h
>> [4]
>> https://code.google.com/p/b-tk/source/browse/#svn%2FBTK%2Ftrunk%2FUtilities%2FCxxTest%2Fcxxtest
>
> Thanks Jean-Christophe. So I guess the short answer is "ctest can't do this".
>
> It seems they have made significant modifications to CxxTest so that
> the bin/cxxtestgen that ships with CxxTest is no longer required
> (they've replaced it with C macros). I like the BTK way better :)
>
> I guess I'm not sure what the advantage of using CTest along with
> something like CxxTest is. If you build an executable using CMake,
> then why not just run ./MyTests from the terminal instead of making an
> interface so that you can call 'ctest'. CTest will report a single
> pass or fail, because as far as it is concerned there is only one
> test, right? The relevant file is
> https://code.google.com/p/b-tk/source/browse/BTK/trunk/Testing/Code/CMakeLists.txt
> that only has one add_test call.
>
> Any thoughts?
>
> David
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake

Sounds like you want to use GTest / GoogleTest, much like SimpleITK does.

See the macro "ADD_GOOGLE_TESTS" in the SimpleITK project, in the file
SimpleITK/Testing/Unit/CMakeLists.txt.

The macro parses the actual test cxx source files, and constructs
"filtered" add_test calls, such that:

- if a cxx file defines 5 tests, add_test is called 5 times, each test
run via add_test runs exactly one of your tests from the cxx file.

There are other projects that are also doing similar things with
gtest. The fairly new open chemistry project MolCore also uses gtest.
That project explicitly lists its tests in the CMakeLists file such
that you'd have to update the CMakeLists.txt file whenever you add a
test case to an existing cxx test source file.

You are correct: ctest does nothing to support this explicitly. (Aside
from cmake providing a FindGTest.cmake module...) An add_test call
tells ctest "run this one test, with this one command line." It's up
to you to write those tests and tell us about them.

But there are fairly easy ways to do what you want, and still drive
the tests, individually, with ctest.


HTH,
David C.


More information about the CMake mailing list