CMake Tests

Last update: 10 Nov 2020 [History] [Edit]

Testing is an important part of software development, this section describes a very simple way to do tests through CMake. Please read up on testing properly after this short introduction, it is an important part of ATLAS software development.


First let’s change directory into source.

cd source

Lets make the configuration a little smarter.

CMake allows you to declare “unit tests” for your code. By convention we tend to put the source files of such tests under the test/ subdirectory in a package.

mkdir MyAnalysis/test

For this exercise let’s create two dummy tests. If you’re looking for inspiration with this, use the following:

#include <iostream>

int main() {
   // Test that basic arithmetics still work:
   if( 1 + 1 == 2 ) {
      std::cout << "Yay!" << std::endl;
      return 0;
   } else {
      std::cout << "Nay." << std::endl;
      return 1;
// Local include(s):
#include "MyAnalysis/MyxAODAnalysis.h"

int main() {

   // Try to instantiate the analysis algorithm. This is more of a compile time check than a runtime one...
   MyxAODAnalysis alg( "Dummy", nullptr );

   return 0;

Please note - if you are using AthAnalysis or any other athena based project, passing a nullptr for the second argument of the constructor of the algorithm will compile but the test will fail because this is not valid.

Let’s call these MyAnalysis/test/test1.cxx and MyAnalysis/test/test2.cxx respectively. Go ahead, and declare them using two calls to atlas_add_test in MyAnalysis/CMakeLists.txt.


atlas_add_test( test1 SOURCES test/test1.cxx )
atlas_add_test( test2 SOURCES test/test2.cxx LINK_LIBRARIES MyAnalysisLib )

Alternatively, to demonstrate how CMake allows you to write a fair bit of logic into your build configuration, let’s use foreach to do the same, in a bit fancier way.

foreach( _test test1 test2 )
   atlas_add_test( ${_test} SOURCES test/${_test}.cxx LINK_LIBRARIES MyAnalysisLib )

Now build your code

cd ../build
cmake ../source

and then run the tests. Running tests you can do in two different ways.

  • The simplest is to just execute make test. This is a very convenient command, and is usually sufficient when you have just a few, quick tests running.
  • You can also execute CTest by hand. This allows you to tweak in great detail how you want to execute your tests. (And which tests you want to execute.) One very useful command, that you may want to remember, is:
ctest -j 4 --output-on-failure

This would run all of your tests, running up to 4 tests in parallel, printing detailed information about all of the failed tests. (So that you don’t only see that the test failed, but also why it failed.)

  • Give both of these a try, and see what happens. Then try to “break” your tests, and check how a failed test shows up with CTest.