testing.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. CMake Testing Guide
  2. *******************
  3. The following is a guide to the CMake test suite for developers.
  4. See documentation on `CMake Development`_ for more information.
  5. See `CMake Integration Testing`_ for running integration testing builds.
  6. See `Tests/README.rst`_ for the test suite layout in the source tree.
  7. .. _`CMake Development`: README.rst
  8. .. _`CMake Integration Testing`: integration-testing.rst
  9. .. _`Tests/README.rst`: ../../Tests/README.rst
  10. Running Tests in the Build Tree
  11. ===============================
  12. After `Building CMake`_, one may run the test suite in the build tree
  13. using `ctest(1)`_:
  14. * With a single-configuration CMake generator, such as ``Ninja``
  15. or ``Unix Makefiles``, one may simply run ``ctest``:
  16. .. code-block:: console
  17. $ ctest
  18. * With a multi-configuration CMake generator, such as
  19. :generator:`Ninja Multi-Config`, :generator:`Visual Studio <Visual Studio
  20. Generators>`, or :generator:`Xcode`, one must tell ``ctest`` which
  21. configuration to test by passing the ``-C <config>`` option:
  22. .. code-block:: console
  23. $ ctest -C Debug
  24. Some useful `ctest(1)`_ options include:
  25. ``-N``
  26. List test names without running them.
  27. ``-V``
  28. Show verbose output from each test.
  29. ``-j <N>``
  30. Run to run up to ``N`` tests concurrently.
  31. ``-R <regex>``
  32. Select tests for which the regular expression matches a substring
  33. of their name.
  34. Cleaning Test Build Trees
  35. -------------------------
  36. Many CMake tests create their own test project build trees underneath
  37. the ``Tests/`` directory at the top of the CMake build tree. These
  38. build trees are left behind after testing completes in order to
  39. facilitate manual investigation of results. Many of the tests do *not*
  40. clean their build trees if they are run again, with the exception of
  41. tests using the `RunCMake`_ infrastructure.
  42. In order to clear test build trees, drive the ``test_clean`` custom target
  43. in the CMake build tree:
  44. .. code-block:: console
  45. $ cmake --build . --target test_clean
  46. This removes the ``Tests/`` subdirectories created by individual tests
  47. so they will use a fresh directory next time they run.
  48. .. _`Building CMake`: ../../README.rst#building-cmake
  49. .. _`ctest(1)`: https://cmake.org/cmake/help/latest/manual/ctest.1.html
  50. .. _`RunCMake`: ../../Tests/RunCMake/README.rst
  51. Running Tests with a Different Generator
  52. ========================================
  53. After `Building CMake`_ with one CMake generator, one may configure the
  54. test suite using a different generator in a separate build tree, without
  55. building CMake itself again, by defining ``CMake_TEST_EXTERNAL_CMAKE``
  56. to be the absolute path to the ``bin`` directory containing the ``cmake``,
  57. ``ctest``, and ``cpack`` executables.
  58. For example, after building CMake with the ``Ninja`` generator:
  59. .. code-block:: console
  60. $ cmake -B build-ninja -G Ninja -DCMAKE_BUILD_TYPE=Debug
  61. $ cmake --build build-ninja
  62. one may configure a second build tree to drive tests with the
  63. ``Ninja Multi-Config`` generator:
  64. .. code-block:: console
  65. $ cmake -B build-nmc-tests -G "Ninja Multi-Config" \
  66. -DCMake_TEST_EXTERNAL_CMAKE="$PWD/build-ninja/bin"
  67. $ cmake --build build-nmc-tests --config Release
  68. The second build tree does not build CMake itself, but does configure
  69. the test suite and build test binaries. One may then run tests normally:
  70. .. code-block:: console
  71. $ cd build-nmc-tests
  72. $ ctest -C Release
  73. Note that the configuration with which one drives tests in the second
  74. build tree is independent of the configuration with which CMake was
  75. built in the first.