# Building all the tests

**URL:** https://discourse.cmake.org/t/building-all-the-tests/5889
**Category:** Usage
**Created:** [June 17, 2022, 8:50pm UTC](https://discourse.cmake.org/t/building-all-the-tests/5889 "2022-06-17T20:50:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Barry\_Revzin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barry_revzin/32/1283_2.png) [@Barry\_Revzin](https://discourse.cmake.org/u/Barry_Revzin)
#### Post date: [June 17, 2022, 8:50pm UTC](https://discourse.cmake.org/t/building-all-the-tests/5889/1 "2022-06-17T20:50:16Z")

</div>

In CMake, for some reason, the `test` rule _only_ runs the tests, it does not build them. If I want to actually compile the tests first, my options seem to be:

- Do `make all && make test` all the time (which requires building more targets than necessary, potentially significantly so)
- Create a new target (like `build-tests`) and have all the test binaries depend on it (which requires effort since you can’t just hijack `add_test` because it doesn’t necessary take a target/binary, and the name of that new target isn’t consistent across repos)

Contrast this to, for instance, `bazel test //...` which just builds and runs all the tests.

Is there a better way in CMake to facilitate building all the tests?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [June 18, 2022, 6:19pm UTC](https://discourse.cmake.org/t/building-all-the-tests/5889/2 "2022-06-18T18:19:39Z")

</div>

Try the [https://cmake.org/cmake/help/latest/manual/ctest.1.html](https://cmake.org/cmake/help/latest/manual/ctest.1.html) Build and Test mode

---

<div class="post-metadata">

### Author: ![Barry\_Revzin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barry_revzin/32/1283_2.png) [@Barry\_Revzin](https://discourse.cmake.org/u/Barry_Revzin)
#### Post date: [June 18, 2022, 8:24pm UTC](https://discourse.cmake.org/t/building-all-the-tests/5889/3 "2022-06-18T20:24:32Z")

</div>

No, I don’t think that’s useful, at least not for what I’m looking for.

I tried this out in a build directory of a cmake project I have that I recently tested, expecting it to just run the tests, but it actually started recompiling _everything_. Also it does so without any output at all, so it took me a while to even figure out that that’s what it was doing.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 20, 2022, 11:11am UTC](https://discourse.cmake.org/t/building-all-the-tests/5889/4 "2022-06-20T11:11:37Z")

</div>

CMake doesn’t support adding dependencies between its built-in targets. Once that issue is fixed, this could at least be done manually. However, CMake doesn’t have a good handle on what tests need what targets beyond its own executable expansion rules.

[https://gitlab.kitware.com/cmake/cmake/-/issues/8438](https://gitlab.kitware.com/cmake/cmake/-/issues/8438)
