Installing and Testing.rst 3.6 KB

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