Debian Codegraph @

3rdparty/catch2/docs/cmake-integration.md

raw symbols SCIP
1 <a id="top"></a>
2 # CMake integration
3
4 **Contents**<br>
5 [CMake targets](#cmake-targets)<br>
6 [Automatic test registration](#automatic-test-registration)<br>
7 [CMake project options](#cmake-project-options)<br>
8 [`CATCH_CONFIG_*` customization options in CMake](#catch_config_-customization-options-in-cmake)<br>
9 [Installing Catch2 from git repository](#installing-catch2-from-git-repository)<br>
10 [Installing Catch2 from vcpkg](#installing-catch2-from-vcpkg)<br>
11
12 Because we use CMake to build Catch2, we also provide a couple of
13 integration points for our users.
14
15 1) Catch2 exports a (namespaced) CMake target
16 2) Catch2's repository contains CMake scripts for automatic registration
17 of `TEST_CASE`s in CTest
18
19 ## CMake targets
20
21 Catch2's CMake build exports two targets, `Catch2::Catch2`, and
22 `Catch2::Catch2WithMain`. If you do not need custom `main` function,
23 you should be using the latter (and only the latter). Linking against
24 it will add the proper include paths and link your target together with
25 2 static libraries that implement Catch2 and its main respectively.
26 If you need custom `main`, you should link only against `Catch2::Catch2`.
27
28 This means that if Catch2 has been installed on the system, it should
29 be enough to do
30 ```cmake
31 find_package(Catch2 3 REQUIRED)
32 # These tests can use the Catch2-provided main
33 add_executable(tests test.cpp)
34 target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
35
36 # These tests need their own main
37 add_executable(custom-main-tests test.cpp test-main.cpp)
38 target_link_libraries(custom-main-tests PRIVATE Catch2::Catch2)
39 ```
40
41 These targets are also provided when Catch2 is used as a subdirectory.
42 Assuming Catch2 has been cloned to `lib/Catch2`, you only need to replace
43 the `find_package` call with `add_subdirectory(lib/Catch2)` and the snippet
44 above still works.
45
46
47 Another possibility is to use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html):
48 ```cmake
49 Include(FetchContent)
50
51 FetchContent_Declare(
52 Catch2
53 GIT_REPOSITORY https://github.com/catchorg/Catch2.git
54 GIT_TAG v3.0.1 # or a later release
55 )
56
57 FetchContent_MakeAvailable(Catch2)
58
59 add_executable(tests test.cpp)
60 target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
61 ```
62
63
64 ## Automatic test registration
65
66 Catch2's repository also contains three CMake scripts that help users
67 with automatically registering their `TEST_CASE`s with CTest. They
68 can be found in the `extras` folder, and are
69
70 1) `Catch.cmake` (and its dependency `CatchAddTests.cmake`)
71 2) `ParseAndAddCatchTests.cmake` (deprecated)
72 3) `CatchShardTests.cmake` (and its dependency `CatchShardTestsImpl.cmake`)
73
74 If Catch2 has been installed in system, both of these can be used after
75 doing `find_package(Catch2 REQUIRED)`. Otherwise you need to add them
76 to your CMake module path.
77
78 <a id="catch_discover_tests"></a>
79 ### `Catch.cmake` and `CatchAddTests.cmake`
80
81 `Catch.cmake` provides function `catch_discover_tests` to get tests from
82 a target. This function works by running the resulting executable with
83 `--list-test-names-only` flag, and then parsing the output to find all
84 existing tests.
85
86 #### Usage
87 ```cmake
88 cmake_minimum_required(VERSION 3.5)
89
90 project(baz LANGUAGES CXX VERSION 0.0.1)
91
92 find_package(Catch2 REQUIRED)
93 add_executable(tests test.cpp)
94 target_link_libraries(tests PRIVATE Catch2::Catch2)
95
96 include(CTest)
97 include(Catch)
98 catch_discover_tests(tests)
99 ```
100
101 When using `FetchContent`, `include(Catch)` will fail unless
102 `CMAKE_MODULE_PATH` is explicitly updated to include the extras
103 directory.
104
105 ```cmake
106 # ... FetchContent ...
107 #
108 list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
109 include(CTest)
110 include(Catch)
111 catch_discover_tests(tests)
112 ```
113
114 #### Customization
115 `catch_discover_tests` can be given several extra arguments:
116 ```cmake
117 catch_discover_tests(target
118 [TEST_SPEC arg1...]
119 [EXTRA_ARGS arg1...]
120 [WORKING_DIRECTORY dir]
121 [TEST_PREFIX prefix]
122 [TEST_SUFFIX suffix]
123 [PROPERTIES name1 value1...]
124 [TEST_LIST var]
125 [REPORTER reporter]
126 [OUTPUT_DIR dir]
127 [OUTPUT_PREFIX prefix]
128 [OUTPUT_SUFFIX suffix]
129 [DISCOVERY_MODE <POST_BUILD|PRE_TEST>]
130 )
131 ```
132
133 * `TEST_SPEC arg1...`
134
135 Specifies test cases, wildcarded test cases, tags and tag expressions to
136 pass to the Catch executable alongside the `--list-test-names-only` flag.
137
138
139 * `EXTRA_ARGS arg1...`
140
141 Any extra arguments to pass on the command line to each test case.
142
143
144 * `WORKING_DIRECTORY dir`
145
146 Specifies the directory in which to run the discovered test cases. If this
147 option is not provided, the current binary directory is used.
148
149
150 * `TEST_PREFIX prefix`
151
152 Specifies a _prefix_ to be added to the name of each discovered test case.
153 This can be useful when the same test executable is being used in multiple
154 calls to `catch_discover_tests()`, with different `TEST_SPEC` or `EXTRA_ARGS`.
155
156
157 * `TEST_SUFFIX suffix`
158
159 Same as `TEST_PREFIX`, except it specific the _suffix_ for the test names.
160 Both `TEST_PREFIX` and `TEST_SUFFIX` can be specified at the same time.
161
162
163 * `PROPERTIES name1 value1...`
164
165 Specifies additional properties to be set on all tests discovered by this
166 invocation of `catch_discover_tests`.
167
168
169 * `TEST_LIST var`
170
171 Make the list of tests available in the variable `var`, rather than the
172 default `<target>_TESTS`. This can be useful when the same test
173 executable is being used in multiple calls to `catch_discover_tests()`.
174 Note that this variable is only available in CTest.
175
176 * `REPORTER reporter`
177
178 Use the specified reporter when running the test case. The reporter will
179 be passed to the test runner as `--reporter reporter`.
180
181 * `OUTPUT_DIR dir`
182
183 If specified, the parameter is passed along as
184 `--out dir/<test_name>` to test executable. The actual file name is the
185 same as the test name. This should be used instead of
186 `EXTRA_ARGS --out foo` to avoid race conditions writing the result output
187 when using parallel test execution.
188
189 * `OUTPUT_PREFIX prefix`
190
191 May be used in conjunction with `OUTPUT_DIR`.
192 If specified, `prefix` is added to each output file name, like so
193 `--out dir/prefix<test_name>`.
194
195 * `OUTPUT_SUFFIX suffix`
196
197 May be used in conjunction with `OUTPUT_DIR`.
198 If specified, `suffix` is added to each output file name, like so
199 `--out dir/<test_name>suffix`. This can be used to add a file extension to
200 the output file name e.g. ".xml".
201
202 * `DISCOVERY_MODE mode`
203
204 If specified allows control over when test discovery is performed.
205 For a value of `POST_BUILD` (default) test discovery is performed at build time.
206 For a a value of `PRE_TEST` test discovery is delayed until just prior to test
207 execution (useful e.g. in cross-compilation environments).
208 ``DISCOVERY_MODE`` defaults to the value of the
209 ``CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE`` variable if it is not passed when
210 calling ``catch_discover_tests``. This provides a mechanism for globally
211 selecting a preferred test discovery behavior.
212
213 ### `ParseAndAddCatchTests.cmake`
214
215 ⚠ This script is [deprecated](https://github.com/catchorg/Catch2/pull/2120)
216 in Catch2 2.13.4 and superseded by the above approach using `catch_discover_tests`.
217 See [#2092](https://github.com/catchorg/Catch2/issues/2092) for details.
218
219 `ParseAndAddCatchTests` works by parsing all implementation files
220 associated with the provided target, and registering them via CTest's
221 `add_test`. This approach has some limitations, such as the fact that
222 commented-out tests will be registered anyway. More serious, only a
223 subset of the assertion macros currently available in Catch can be
224 detected by this script and tests with any macros that cannot be
225 parsed are *silently ignored*.
226
227
228 #### Usage
229
230 ```cmake
231 cmake_minimum_required(VERSION 3.5)
232
233 project(baz LANGUAGES CXX VERSION 0.0.1)
234
235 find_package(Catch2 REQUIRED)
236 add_executable(tests test.cpp)
237 target_link_libraries(tests PRIVATE Catch2::Catch2)
238
239 include(CTest)
240 include(ParseAndAddCatchTests)
241 ParseAndAddCatchTests(tests)
242 ```
243
244
245 #### Customization
246
247 `ParseAndAddCatchTests` provides some customization points:
248 * `PARSE_CATCH_TESTS_VERBOSE` -- When `ON`, the script prints debug
249 messages. Defaults to `OFF`.
250 * `PARSE_CATCH_TESTS_NO_HIDDEN_TESTS` -- When `ON`, hidden tests (tests
251 tagged with either of `[.]` or `[.foo]`) will not be registered.
252 Defaults to `OFF`.
253 * `PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME` -- When `ON`, adds fixture
254 class name to the test name in CTest. Defaults to `ON`.
255 * `PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME` -- When `ON`, adds target
256 name to the test name in CTest. Defaults to `ON`.
257 * `PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS` -- When `ON`, adds test
258 file to `CMAKE_CONFIGURE_DEPENDS`. This means that the CMake configuration
259 step will be re-ran when the test files change, letting new tests be
260 automatically discovered. Defaults to `OFF`.
261
262
263 Optionally, one can specify a launching command to run tests by setting the
264 variable `OptionalCatchTestLauncher` before calling `ParseAndAddCatchTests`. For
265 instance to run some tests using `MPI` and other sequentially, one can write
266 ```cmake
267 set(OptionalCatchTestLauncher ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${NUMPROC})
268 ParseAndAddCatchTests(mpi_foo)
269 unset(OptionalCatchTestLauncher)
270 ParseAndAddCatchTests(bar)
271 ```
272
273
274 ### `CatchShardTests.cmake`
275
276 > `CatchShardTests.cmake` was introduced in Catch2 3.1.0.
277
278 `CatchShardTests.cmake` provides a function
279 `catch_add_sharded_tests(TEST_BINARY)` that splits tests from `TEST_BINARY`
280 into multiple shards. The tests in each shard and their order is randomized,
281 and the seed changes every invocation of CTest.
282
283 Currently there are 3 customization points for this script:
284
285 * SHARD_COUNT - number of shards to split target's tests into
286 * REPORTER - reporter spec to use for tests
287 * TEST_SPEC - test spec used for filtering tests
288
289 Example usage:
290
291 ```
292 include(CatchShardTests)
293
294 catch_add_sharded_tests(foo-tests
295 SHARD_COUNT 4
296 REPORTER "xml::out=-"
297 TEST_SPEC "A"
298 )
299
300 catch_add_sharded_tests(tests
301 SHARD_COUNT 8
302 REPORTER "xml::out=-"
303 TEST_SPEC "B"
304 )
305 ```
306
307 This registers total of 12 CTest tests (4 + 8 shards) to run shards
308 from `foo-tests` test binary, filtered by a test spec.
309
310 _Note that this script is currently a proof-of-concept for reseeding
311 shards per CTest run, and thus does not support (nor does it currently
312 aim to support) all customization points from
313 [`catch_discover_tests`](#catch_discover_tests)._
314
315
316 ## CMake project options
317
318 Catch2's CMake project also provides some options for other projects
319 that consume it. These are:
320
321 * `BUILD_TESTING` -- When `ON` and the project is not used as a subproject,
322 Catch2's test binary will be built. Defaults to `ON`.
323 * `CATCH_INSTALL_DOCS` -- When `ON`, Catch2's documentation will be
324 included in the installation. Defaults to `ON`.
325 * `CATCH_INSTALL_EXTRAS` -- When `ON`, Catch2's extras folder (the CMake
326 scripts mentioned above, debugger helpers) will be included in the
327 installation. Defaults to `ON`.
328 * `CATCH_DEVELOPMENT_BUILD` -- When `ON`, configures the build for development
329 of Catch2. This means enabling test projects, warnings and so on.
330 Defaults to `OFF`.
331
332
333 Enabling `CATCH_DEVELOPMENT_BUILD` also enables further configuration
334 customization options:
335
336 * `CATCH_BUILD_TESTING` -- When `ON`, Catch2's SelfTest project will be
337 built. Defaults to `ON`. Note that Catch2 also obeys `BUILD_TESTING` CMake
338 variable, so _both_ of them need to be `ON` for the SelfTest to be built,
339 and either of them can be set to `OFF` to disable building SelfTest.
340 * `CATCH_BUILD_EXAMPLES` -- When `ON`, Catch2's usage examples will be
341 built. Defaults to `OFF`.
342 * `CATCH_BUILD_EXTRA_TESTS` -- When `ON`, Catch2's extra tests will be
343 built. Defaults to `OFF`.
344 * `CATCH_BUILD_FUZZERS` -- When `ON`, Catch2 fuzzing entry points will
345 be built. Defaults to `OFF`.
346 * `CATCH_ENABLE_WERROR` -- When `ON`, adds `-Werror` or equivalent flag
347 to the compilation. Defaults to `ON`.
348 * `CATCH_BUILD_SURROGATES` -- When `ON`, each header in Catch2 will be
349 compiled separately to ensure that they are self-sufficient.
350 Defaults to `OFF`.
351
352
353 ## `CATCH_CONFIG_*` customization options in CMake
354
355 > CMake support for `CATCH_CONFIG_*` options was introduced in Catch2 3.0.1
356
357 Due to the new separate compilation model, all the options from the
358 [Compile-time configuration docs](configuration.md#top) can also be set
359 through Catch2's CMake. To set them, define the option you want as `ON`,
360 e.g. `-DCATCH_CONFIG_NOSTDOUT=ON`.
361
362 Note that setting the option to `OFF` doesn't disable it. To force disable
363 an option, you need to set the `_NO_` form of it to `ON`, e.g.
364 `-DCATCH_CONFIG_NO_COLOUR_WIN32=ON`.
365
366
367 To summarize the configuration option behaviour with an example:
368
369 | `-DCATCH_CONFIG_COLOUR_WIN32` | `-DCATCH_CONFIG_NO_COLOUR_WIN32` | Result |
370 |-------------------------------|----------------------------------|-------------|
371 | `ON` | `ON` | error |
372 | `ON` | `OFF` | force-on |
373 | `OFF` | `ON` | force-off |
374 | `OFF` | `OFF` | auto-detect |
375
376
377
378 ## Installing Catch2 from git repository
379
380 If you cannot install Catch2 from a package manager (e.g. Ubuntu 16.04
381 provides catch only in version 1.2.0) you might want to install it from
382 the repository instead. Assuming you have enough rights, you can just
383 install it to the default location, like so:
384 ```
385 $ git clone https://github.com/catchorg/Catch2.git
386 $ cd Catch2
387 $ cmake -Bbuild -H. -DBUILD_TESTING=OFF
388 $ sudo cmake --build build/ --target install
389 ```
390
391 If you do not have superuser rights, you will also need to specify
392 [CMAKE_INSTALL_PREFIX](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html)
393 when configuring the build, and then modify your calls to
394 [find_package](https://cmake.org/cmake/help/latest/command/find_package.html)
395 accordingly.
396
397 ## Installing Catch2 from vcpkg
398
399 Alternatively, you can build and install Catch2 using [vcpkg](https://github.com/microsoft/vcpkg/) dependency manager:
400 ```
401 git clone https://github.com/Microsoft/vcpkg.git
402 cd vcpkg
403 ./bootstrap-vcpkg.sh
404 ./vcpkg integrate install
405 ./vcpkg install catch2
406 ```
407
408 The catch2 port in vcpkg is kept up to date by microsoft team members and community contributors.
409 If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
410
411 ---
412
413 [Home](Readme.md#top)