NA-MIC-kit-curriculum/Testing-Based Programming/How to add Tests in CMake
From NAMIC Wiki
Home < NA-MIC-kit-curriculum < Testing-Based Programming < How to add Tests in CMake
This is a basic tutorial on how to set up testing in your project.
We start with a very simple case here, and progressively will add more interesting features
Contents
Hello World
We start by creating a minimal project with only
- main.cxx
- CMakeLists.txt
main.cxx
Here is the main file
#include <iostream> #include <stdlib.h> int main( int argc, char * argv [] ) { const unsigned int N = atoi( argv[1] ); for( unsigned int i=0; i < N; i++ ) { std::cout << "Hello World ! " << i << std::endl; } return 0; }
CMakeLists.txt
The key commands for testing are
- INCLUDE(CTest)
- ADD_TEST(testname executable arg1 arg2 arg3 ... argN)
Here is the minimal CMakeLists.txt file
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(TestingTutorial) INCLUDE(CTest) ADD_EXECUTABLE(TestMain main.cxx) ADD_TEST(test1 TestMain 5) ADD_TEST(test2 TestMain 8)
Building
Create source directory
Put the two files
- main.cxx
- CMakeLists.txt
in a directory for source code. We will refer to this directory as the "SOURCE_DIR" hereafter.
Create binary directory
Create a directory for compiling the code. We will refer to this directory as the "BINARY_DIR" hereafter.
Then do the following
cd ${BINARY_DIR} cmake ${SOURCE_DIR} make make test