Difference between revisions of "NA-MIC-kit-curriculum/Testing-Based Programming/How to add Tests in CMake"
From NAMIC Wiki
Line 57: | Line 57: | ||
Create a directory for compiling the code. | Create a directory for compiling the code. | ||
We will refer to this directory as the "BINARY_DIR" hereafter. | We will refer to this directory as the "BINARY_DIR" hereafter. | ||
+ | |||
+ | == Configure, Build and Test == | ||
Then do the following | Then do the following |
Revision as of 14:32, 12 December 2009
Home < NA-MIC-kit-curriculum < Testing-Based Programming < How to add Tests in CMakeThis 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.
Configure, Build and Test
Then do the following
cd ${BINARY_DIR} cmake ${SOURCE_DIR} make make test
at this point you should see an output similar to
Running tests... Test project /home/ibanez/src/test/CMake/bin Start 1: test1 1/2 Test #1: test1 ............................ Passed 0.00 sec Start 2: test2 2/2 Test #2: test2 ............................ Passed 0.00 sec .. 100% tests passed, 0 tests failed out of 2 .. Total Test time (real) = 0.01 sec