Running Tests

Setup And Usage

  • Enable testing (ideally in toplavel CMakeLists.txt)

    ENABLE_TESTING()
    
  • This creates a test target

    $ cmake /home/jfasch/work/jfasch-home/trainings/material/soup/cmake/14-testing/
    $ make test
    Running tests...
    Test project /home/jfasch/tmp/cmake-demo
    No tests were found!!!
    

Add Simple Test

  • ADD_TEST() adds a program to run when test target is run

  • Exit status 0 ⟶ Ok

  • Exit status not 0 ⟶ Failure

bin/CMakeLists.txt
ADD_TEST(
  NAME hello-first-runs
  COMMAND hello-first)
$ make test
Running tests...
Test project /home/jfasch/tmp/cmake-demo
    Start 1: hello-first-runs
1/1 Test #1: hello-first-runs .................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec

Checking Test Output

ADD_TEST(
  NAME hello-first-has-version
  COMMAND hello-first)
SET_TESTS_PROPERTIES(
  hello-first-has-version
  PROPERTIES PASS_REGULAR_EXPRESSION "Hello World")
$ make test
Running tests...
Test project /home/jfasch/tmp/cmake-demo
    Start 1: hello-first-runs
1/2 Test #1: hello-first-runs .................   Passed    0.00 sec
    Start 2: hello-first-has-version
2/2 Test #2: hello-first-has-version ..........   Passed    0.00 sec

100% tests passed, 0 tests failed out of 2

Total Test time (real) =   0.01 sec

Questionability

  • Is this a testing framework?

  • Probably not

  • CTest (the CMake module that implements testing) integrates nicely with CDash and other products of KitWare

  • ⟶ A clumsy try of vendor lock-in, I’d say

Better options …