Installing and Testing.rst 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. Step 4: Installing and Testing
  2. ==============================
  3. Now we can start adding install rules and testing support to our project.
  4. Install Rules
  5. -------------
  6. The install rules are fairly simple: for MathFunctions we want to install the
  7. library and header file and for the application we want to install the
  8. executable and configured header.
  9. So to the end of ``MathFunctions/CMakeLists.txt`` we add:
  10. .. literalinclude:: Step5/MathFunctions/CMakeLists.txt
  11. :language: cmake
  12. :start-after: # install rules
  13. And to the end of the top-level ``CMakeLists.txt`` we add:
  14. .. literalinclude:: Step5/CMakeLists.txt
  15. :language: cmake
  16. :start-after: # add the install targets
  17. :end-before: # enable testing
  18. That is all that is needed to create a basic local install of the tutorial.
  19. Now run the :manual:`cmake <cmake(1)>` executable or the
  20. :manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it
  21. with your chosen build tool.
  22. Then run the install step by using the ``install`` option of the
  23. :manual:`cmake <cmake(1)>` command (introduced in 3.15, older versions of
  24. CMake must use ``make install``) from the command line. For
  25. multi-configuration tools, don't forget to use the ``--config`` argument to
  26. specify the configuration. If using an IDE, simply build the ``INSTALL``
  27. target. This step will install the appropriate header files, libraries, and
  28. executables. For example:
  29. .. code-block:: console
  30. cmake --install .
  31. The CMake variable :variable:`CMAKE_INSTALL_PREFIX` is used to determine the
  32. root of where the files will be installed. If using the ``cmake --install``
  33. command, the installation prefix can be overridden via the ``--prefix``
  34. argument. For example:
  35. .. code-block:: console
  36. cmake --install . --prefix "/home/myuser/installdir"
  37. Navigate to the install directory and verify that the installed Tutorial runs.
  38. Testing Support
  39. ---------------
  40. Next let's test our application. At the end of the top-level ``CMakeLists.txt``
  41. file we can enable testing and then add a number of basic tests to verify that
  42. the application is working correctly.
  43. .. literalinclude:: Step5/CMakeLists.txt
  44. :language: cmake
  45. :start-after: # enable testing
  46. The first test simply verifies that the application runs, does not segfault or
  47. otherwise crash, and has a zero return value. This is the basic form of a
  48. CTest test.
  49. The next test makes use of the :prop_test:`PASS_REGULAR_EXPRESSION` test
  50. property to verify that the output of the test contains certain strings. In
  51. this case, verifying that the usage message is printed when an incorrect number
  52. of arguments are provided.
  53. Lastly, we have a function called ``do_test`` that runs the application and
  54. verifies that the computed square root is correct for given input. For each
  55. invocation of ``do_test``, another test is added to the project with a name,
  56. input, and expected results based on the passed arguments.
  57. Rebuild the application and then cd to the binary directory and run the
  58. :manual:`ctest <ctest(1)>` executable: ``ctest -N`` and ``ctest -VV``. For
  59. multi-config generators (e.g. Visual Studio), the configuration type must be
  60. specified. To run tests in Debug mode, for example, use ``ctest -C Debug -VV``
  61. from the build directory (not the Debug subdirectory!). Alternatively, build
  62. the ``RUN_TESTS`` target from the IDE.