A CI job must run a flaky GoogleTest subset 100 times in random order. Which flags do that, and how do you replay the exact order of a failing iteration?
answer
- repeat, shuffle, seed
- the seed is printed per iteration
- next iteration, seed plus one
- same process, same statics
basics
~10 sRun with --gtest_filter=<subset> --gtest_repeat=100 --gtest_shuffle. Each iteration prints its seed, and the seed rises by one per iteration. Replay a failure with the same filter, --gtest_shuffle and --gtest_random_seed set to that iteration's printed seed.
solid answer
~40 s`--gtest_repeat=N` runs the selected tests N times in **one process** (negative means forever) and does not stop at failures; the exit status is 1 if any iteration failed. `--gtest_shuffle` randomises suite and test order; without `--gtest_random_seed` the seed comes from the clock, and each iteration prints `Note: Randomizing tests' orders with a seed of N .`. In GoogleTest 1.18 the next iteration's seed is the previous one plus one, wrapping from 99999 to 1, and the order is restored between iterations, so any iteration can be replayed alone: same filter, `--gtest_shuffle --gtest_random_seed=<printed seed>`. `--gtest_break_on_failure` stops at the first failure, under a debugger if attached. Because iterations share a process, static state and global environments carry over; in 1.18 environments are set up once unless `--gtest_recreate_environments_when_repeating` is given.
code
console · 21 lines$ ./flaky_test --gtest_filter='Flaky.*' --gtest_repeat=3 --gtest_shuffle \
--gtest_random_seed=42 --gtest_output=xml:reports/
Repeating all tests (iteration 1) . . .
Note: Randomizing tests' orders with a seed of 42 .
[ RUN ] Flaky.AlwaysPasses
[ RUN ] Flaky.FailsFirstTime
[ FAILED ] Flaky.FailsFirstTime (0 ms)
Repeating all tests (iteration 2) . . .
Note: Randomizing tests' orders with a seed of 43 .
[ RUN ] Flaky.FailsFirstTime
[ RUN ] Flaky.AlwaysPasses
[ PASSED ] 2 tests.
Repeating all tests (iteration 3) . . .
Note: Randomizing tests' orders with a seed of 44 .
...
$ echo $?
1
$ ./flaky_test --gtest_filter='Flaky.*' --gtest_shuffle --gtest_random_seed=43
Note: Randomizing tests' orders with a seed of 43 .
[ RUN ] Flaky.FailsFirstTime
[ RUN ] Flaky.AlwaysPassesgo deeper
Know that --gtest_repeat reruns tests and --gtest_shuffle randomises their order.
Explain the printed seed, --gtest_random_seed and how to replay one iteration with the same filter.
Recognise state carried across iterations of one process, global environments that are set up once, and when to use separate processes instead.
Decide how the pipeline quarantines and re-runs flaky tests so repeated runs produce evidence rather than noise.
## The three flags Hunting a test that fails one run in fifty is a GoogleTest runner job, and three flags do the work: - **`--gtest_repeat=N`** runs every selected test N times. It does not stop at failures; `-1` (any negative count) repeats forever. Environment variable: `GTEST_REPEAT`. - **`--gtest_shuffle`** runs the tests in random order, which exposes tests that depend on state left behind by others. Environment variable: `GTEST_SHUFFLE=1`. - **`--gtest_random_seed=SEED`** fixes the shuffle seed, an integer from 0 to 99999. `0` is special and means "derive the seed from the current time", which is also the default. The CI command for the scenario is therefore: ```bash ./flaky_test --gtest_filter='Flaky.*' --gtest_repeat=100 --gtest_shuffle \ --gtest_output=xml:reports/ ``` ## How the seed advances Every iteration prints the seed it used. The documentation only says GoogleTest picks a different seed and reshuffles in each iteration; the 1.18 source is more specific. After each iteration it calls `GetNextRandomSeed`, which returns the seed **plus one** and wraps from 99999 back to 1. The example run started at 42 and printed 42, 43 and 44. Running with `--gtest_random_seed=99999` prints 99999 and then 1. Two more details make replay reliable: 1. after each iteration GoogleTest **restores the original order** before shuffling again, so iteration k depends only on its own seed, not on the k-1 shuffles before it; 2. the shuffle permutes the tests that were *selected*, so the replay must use the **same filter**. To replay iteration 2 of the example: `./flaky_test --gtest_filter='Flaky.*' --gtest_shuffle --gtest_random_seed=43`. The printed order, `FailsFirstTime` then `AlwaysPasses`, matches iteration 2 exactly. ## Exit status and stopping The process exits with status **1 if any iteration failed**, even when the last 99 passed; the example's first iteration failed and the run still ended with status 1. Useful companions: - `--gtest_break_on_failure` turns the first failure into a breakpoint, so running under `gdb` or `lldb` stops at the failing assertion; without a debugger the process stops there; - `--gtest_fail_fast` skips the rest of an iteration after its first failing test; with `--gtest_repeat`, the later iterations still run. ## The trap: one process, shared state All iterations run inside the **same process**. That is what makes `--gtest_repeat` cheap, and also what makes its results subtle: - **static and global state persists.** In the example, `FailsFirstTime` increments a static counter and fails only on the first call; every later iteration passes. A test that leaks state into a singleton can fail only in iteration 1, or only after iteration 1; - **global environments are set up once.** In 1.18 the `--gtest_recreate_environments_when_repeating` flag defaults to `false`, so a global `Environment` is set up before the first iteration and torn down after the last; pass the flag to recreate it each time. GoogleTest's advanced guide still says the opposite, that set-up repeats by default and that you pass `=false` to avoid it; in 1.18 a run with three repeats calls a global environment's `SetUp()` once without the flag and three times with it. When `--gtest_repeat` is negative, environments are always recreated. If the flakiness is a race with another process or a leaking singleton, a fresh process per attempt is the stronger experiment: run the binary 100 times from a shell loop, each with its own seed, instead of one process with `--gtest_repeat=100`. ## What the report shows An XML report written with `--gtest_output` is rewritten at the end of each iteration, so after a repeated run it describes only the **last** iteration. The exit status, or the console log, is the authority on whether any iteration failed. ## A CI recipe 1. Pin the subset in one variable: `FILTER='Flaky.*'`. 2. Run `./flaky_test --gtest_filter="$FILTER" --gtest_repeat=100 --gtest_shuffle` and keep the full console log as a job artefact. 3. Fail the job on a non-zero exit status, not on the XML. 4. On failure, search the log for the first failing iteration and the seed printed just above it. 5. Reproduce locally with `--gtest_filter="$FILTER" --gtest_shuffle --gtest_random_seed=<seed>`, adding `--gtest_break_on_failure` under a debugger. 6. If the replay passes, suspect state carried over from earlier iterations and repeat the experiment as separate processes.
- The flaky test fails in iteration 37 of a run that started from a clock-derived seed. How do you get the seed without the start value?Read it from the log: every iteration prints `Note: Randomizing tests' orders with a seed of N .` just after `Repeating all tests (iteration 37)`. The XML report also carries a `random_seed` attribute, but only for the last iteration. Then run the same filter with `--gtest_shuffle --gtest_random_seed=N`, under a debugger with `--gtest_break_on_failure` if needed.
- Why can --gtest_repeat=100 pass a test that fails in 1 of 100 fresh CI runs?Iterations share one process, so whatever the first run initialised, such as a static cache, a singleton or a global environment, is still there in the next 99. If the failure depends on cold start-up state, only the first iteration can see it. Re-launching the binary per attempt gives every attempt a cold start.
- Assuming each --gtest_repeat iteration starts a fresh process
- Expecting --gtest_repeat to stop at the first failure by default
- Replaying a shuffled failure without the same filter
- Thinking --gtest_random_seed=0 means no shuffling
- Believing the exit status reflects only the last iteration
Assumes GoogleTest 1.18: the shuffle seed advances by one per repeated iteration and global environments are set up once unless --gtest_recreate_environments_when_repeating is given.