Installing and Testing.rst 3.4 KB

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