Skip to content

Conversation

@jandubois
Copy link
Member

@jandubois jandubois commented Oct 18, 2025

It will rerun all tests up to this number on failure.

Also provides a flaky function to set the retries to LIMA_BATS_FLAKY_TESTS_RETRIES to allow a greater number of retries to known flaky tests.

So in CI you could set both, to e.g. retry all tests at least once, but retry flaky tests up to 5 times.

cat hack/bats/tests/chaos-monkey.bats
load "../helpers/load"

@test "flaky test that fails randomly" {
    flaky
    echo "Attempt ${BATS_TEST_TRY_NUMBER}/$((BATS_TEST_RETRIES + 1))" >&3
    ((RANDOM % 10 < 3))
}LIMA_BATS_ALL_TESTS_RETRIES=2 ./hack/bats/lib/bats-core/bin/bats -T ./hack/bats/tests/chaos-monkey.bats
chaos-monkey.bats
   flaky test that fails randomly                                                                                                                           1/1 in 0 sec
Attempt 1/3
   flaky test that fails randomly                                                                                                                           1/1 in 0 sec
Attempt 2/3
 ✗ flaky test that fails randomly [4]
Attempt 3/3
   (in test file hack/bats/tests/chaos-monkey.bats, line 6)
     `(( RANDOM % 10 < 3))' failed

1 test, 1 failure in 0 secondsLIMA_BATS_FLAKY_TESTS_RETRIES=7 ./hack/bats/lib/bats-core/bin/bats -T ./hack/bats/tests/chaos-monkey.bats
chaos-monkey.bats
   flaky test that fails randomly                                                                                                                           1/1 in 0 sec
Attempt 1/8
   flaky test that fails randomly                                                                                                                           1/1 in 1 sec
Attempt 2/8
 ✓ flaky test that fails randomly [5]
Attempt 3/8

1 test, 0 failures in 1 seconds

It will rerun all tests up to this number on failure.

Also provides a `flaky` function to set the retries to
LIMA_BATS_FLAKY_TESTS_RETRIES to allow a greater number
of retries to known flaky tests.

Signed-off-by: Jan Dubois <jan.dubois@suse.com>
@jandubois
Copy link
Member Author

Should we have a default LIMA_BATS_FLAKY_TESTS_RETRIES=2 so we always allow up to 3 attempts for tests calling flaky by default?


# BATS_TEST_RETRIES must be set for the individual test and cannot be imported from the
# parent environment because the BATS test runner sets it to 0 before running the test.
BATS_TEST_RETRIES=${LIMA_BATS_ALL_TESTS_RETRIES:-0}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also update

- name: "Run BATS k8s tests"
# Wish we could use BATS_TEST_RETRIES=3 as an environment variable here,
# but bats does not seem to support it.
uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
with:
timeout_minutes: 30
retry_on: error
max_attempts: 3
command: ./hack/bats/lib/bats-core/bin/bats --timing ./hack/bats/extras/k8s.bats

@AkihiroSuda
Copy link
Member

Should we have a default LIMA_BATS_FLAKY_TESTS_RETRIES=2 so we always allow up to 3 attempts for tests calling flaky by default?

No, by default the test shouldn't be flaky.

@AkihiroSuda AkihiroSuda added this to the v2.0.0 milestone Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants