# How to define fixture that executes once, no matter where I run \`ctest\`?

**URL:** <https://discourse.cmake.org/t/how-to-define-fixture-that-executes-once-no-matter-where-i-run-ctest/3978>\
**Category:** Code\
**Created:** [August 25, 2021, 12:34pm UTC](https://discourse.cmake.org/t/how-to-define-fixture-that-executes-once-no-matter-where-i-run-ctest/3978 "2021-08-25T12:34:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brettdh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brettdh/32/1731_2.png) [@brettdh](https://discourse.cmake.org/u/brettdh)\
**Post date:** [August 25, 2021, 12:34pm UTC](https://discourse.cmake.org/t/how-to-define-fixture-that-executes-once-no-matter-where-i-run-ctest/3978/1 "2021-08-25T12:34:02Z")

</div>

I have a test fixture for starting a database server:

```auto
# pg_setup.cmake

add_test(NAME setupPostgres COMMAND /path/to/pg-setup)
add_test(NAME teardownPostgres COMMAND /path/to/pg-teardown)

set_tests_properties(setupPostgres PROPERTIES FIXTURES_SETUP postgresFixture)
set_tests_properties(teardownPostgres PROPERTIES FIXTURES_CLEANUP postgresFixture)

```

and several other tests that use the fixture, like this:

```auto
include(pg_setup)
add_test(NAME foo ...)
set_tests_properties(foo PROPERTIES FIXTURES_REQUIRED postgresFixture)

```

These tests are in different directories, sprinkled throughout a large source tree, with multiple `CMakeLists.txt` files defining tests that depend on `postgresFixture` in this way.

When I run `ctest` in one of these directories, things work as expected:

1. `setupPostgres` runs before all tests that require it
2. `teardownPostgres` runs after all tests that require it
3. All tests in between run in parallel when I use `--parallel`

However, when I run `ctest` at the top level, I see multiple instances of `setupPostgres` running in parallel, which is not what I want. How can I avoid this?

---

<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:** [August 25, 2021, 11:41pm UTC](https://discourse.cmake.org/t/how-to-define-fixture-that-executes-once-no-matter-where-i-run-ctest/3978/2 "2021-08-25T23:41:59Z")

</div>

That sounds unexpected. If you can reduce your problem down to a minimal example project which demonstrates the issue, I can investigate. Without that, it will be a bit hard to do much.
