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

I have a test fixture for starting a database server:

# 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:

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?

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.