# Installing and Testing #

Now we can start adding testing support and install rules to our project.

The install rules are fairly simple; for MathFunctions we install the library
and header file, for the application we install the executable and configured
header.

So to MathFunctions/CMakeLists.txt we add:

  install (TARGETS MathFunctions DESTINATION bin)
  install (FILES MathFunctions.h DESTINATION include)

And the to top-level CMakeLists.txt we add:

  install(TARGETS Tutorial DESTINATION bin)
  install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
          DESTINATION include
          )

That is all that is needed to create a basic local install of the tutorial.

Run cmake or cmake-gui to configure the project and then build it with your
chosen build tool. Then build the “install” target by typing 'make install'
from the command line or build the INSTALL target from an IDE. This will
install the appropriate header files, libraries, and executables.

Verify that the installed Tutorial runs. Note: The CMake variable
CMAKE_INSTALL_PREFIX is used to determine the root of where the files will
be installed.

Next let's test our application. Adding testing is an easy process. At the
end of the top-level CMakeLists file we can add a number of basic tests to
verify that the application is working correctly.

  # enable testing
  enable_testing()

  # does the application run
  add_test(NAME Runs COMMAND Tutorial 25)

  # does the usage message work?
  add_test(NAME Usage COMMAND Tutorial)
  set_tests_properties(Usage
    PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
    )

  # define a function to simplify adding tests
  function(do_test target arg result)
    add_test(NAME Comp${arg} COMMAND ${target} ${arg})
    set_tests_properties(Comp${arg}
      PROPERTIES PASS_REGULAR_EXPRESSION ${result}
      )
  endfunction(do_test)

  # do a bunch of result based tests
  do_test(Tutorial 25 "25 is 5")
  do_test(Tutorial -25 "-25 is [-nan|nan|0]")
  do_test(Tutorial 0.0001 "0.0001 is 0.01")

The first test simply verifies that the application runs, does not segfault or
otherwise crash, and has a zero return value. This is the basic form of a CTest
test.

The Usage test uses a regular expression to verify that the usage message
is printed when an incorrect number of arguments are provided.

Lastly, we have a function called do_test that simplifies running the
application and verifying that the computed square root is correct for given
input.

To run tests, cd to the binary directory and run “ctest -N” and “ctest -VV”.
