Building all the tests

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?

Try the Build and Test mode

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.

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.