# ctest presets and --build-and-test

**URL:** https://discourse.cmake.org/t/ctest-presets-and-build-and-test/7514
**Category:** Usage
**Tags:** tool:ctest
**Created:** [February 21, 2023, 9:34pm UTC](https://discourse.cmake.org/t/ctest-presets-and-build-and-test/7514 "2023-02-21T21:34:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kisielk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kisielk/32/2765_2.png) [@kisielk](https://discourse.cmake.org/u/kisielk)
#### Post date: [February 21, 2023, 9:34pm UTC](https://discourse.cmake.org/t/ctest-presets-and-build-and-test/7514/1 "2023-02-21T21:34:23Z")

</div>

I’m trying to use the `--build-and-test` option with `ctest` in conjunction with presets. I’m using Ninja Multi-Config as my generator. My test presets include both the `configurePreset` and `configuration` fields yet when I try to use `ctest --build-and-test --preset=mypreset` I still get:

```
--build-and-test requires that the generator be provided using the --build-generator command line option. 

```

It seems based on the information in the preset that `ctest` should be able to figure this out on its own, any reason it doesn’t?

---

<div class="post-metadata">

### Author: ![kisielk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kisielk/32/2765_2.png) [@kisielk](https://discourse.cmake.org/u/kisielk)
#### Post date: [June 6, 2024, 9:51pm UTC](https://discourse.cmake.org/t/ctest-presets-and-build-and-test/7514/2 "2024-06-06T21:51:16Z")

</div>

I ran into this scenario again today and did a Google search… my own post was the first result! Does anyone have any advice on how to achieve this yet?

Basically I currently have to do:

```auto
cmake --build --preset foo-release && ctest --preset foo-release # etc

```

Is there a way to have ctest perform the build for the corresponding build preset without having to chain these two commands?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [June 7, 2024, 4:15am UTC](https://discourse.cmake.org/t/ctest-presets-and-build-and-test/7514/3 "2024-06-07T04:15:50Z")

</div>

Test presets are not intended to run `--build-and-test` invocations. They are only meant for running test cases defined by the project through `add_test()` and so forth. If you want to execute a test case that runs `ctest --build-and-test`, you need to wrap that as a test case of the main project. For example:

```cmake
add_test(NAME blah
    COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test .... # the rest of your arguments here

```

---

<div class="post-metadata">

### Author: ![kisielk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kisielk/32/2765_2.png) [@kisielk](https://discourse.cmake.org/u/kisielk)
#### Post date: [June 11, 2024, 4:59pm UTC](https://discourse.cmake.org/t/ctest-presets-and-build-and-test/7514/4 "2024-06-11T16:59:56Z")

</div>

@craig.scott hm ok, maybe I’ve misunderstood the purpose of `--build-and-test`. I re-read the section in your book and I think I get how it should be used. It seems workflow presets would achieve what I am looking for.
