# How to mimic \`make check\` behavior

**URL:** https://discourse.cmake.org/t/how-to-mimic-make-check-behavior/13462
**Category:** Code
**Tags:** gen:makefiles
**Created:** [January 22, 2025, 11:33pm UTC](https://discourse.cmake.org/t/how-to-mimic-make-check-behavior/13462 "2025-01-22T23:33:52Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![vbsteph](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vbsteph](https://discourse.cmake.org/u/vbsteph)
#### Post date: [January 22, 2025, 11:33pm UTC](https://discourse.cmake.org/t/how-to-mimic-make-check-behavior/13462/1 "2025-01-22T23:33:52Z")

</div>

I want to use CMake to mimic the behavior of a testing workflow that uses `make check` (as generated by Autotools).

Let’s say I have two libraries, A and B, where B depends on A. If I run `make check` from the binary directory of A, it should build the unit tests for A and then run them. If I run `make check` from the binary directory of B, it should build the unit tests for B and run them. But if I run `make check` from a parent binary directory that contains both A and B, I want it to detect that B depends on A, build and run A’s unit tests, and then build and run B’s unit tests.

I’ve run into a handful of issues replicating this workflow, and I could use some advice.

I can create a custom target `check` in my top-level `CMakeLists.txt` that depends on my project’s test executables and give it a command to run ctest. The resulting target, however, can only be run from the top level of the binary tree corresponding to its source location. I can’t access the target if I descend any further into the binary tree. Further, if I invoke it from the top of the binary tree, it will trigger ALL the test targets to build and then run ALL those test targets rather than going package-by-package.

I could achieve the package-by-package movement behavior with separate custom targets in each package, but that still doesn’t let me run `make check` from wherever I want in the binary tree the same way I could with `ctest`. CTest’s build-and-test mode might do what I need, but the docs imply that I wouldn’t get the package-by-package behavior I want. Same goes for workflow presets. It’d be ideal if there were a `ctest` option that told it to build the tests before running them, but I see no such option.

I feel like the solution is obvious but I’ve been staring at the problem for too long, so I could use advice on the best way to proceed. Are there CTest options I don’t know about? Is build-and-test mode the way to go? Generator expressions that would help? Something to do with the `make test` target? Anything is helpful.
