Difference between revisions of "NA-MIC-kit-curriculum/Testing-Based Programming/How to add Tests in CMake"

From NAMIC Wiki
Jump to: navigation, search
Line 16: Line 16:
 
   #include <iostream>
 
   #include <iostream>
 
   #include <stdlib.h>
 
   #include <stdlib.h>
 
 
   int main( int argc, char * argv [] )
 
   int main( int argc, char * argv [] )
 
   {
 
   {
 
     const unsigned int N = atoi( argv[1] );
 
     const unsigned int N = atoi( argv[1] );
 
 
     for( unsigned int i=0; i < N; i++ )
 
     for( unsigned int i=0; i < N; i++ )
 
     {
 
     {

Revision as of 14:26, 12 December 2009

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

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)