The MailStack Api (Email Testing) is now available!

How to repeat a command until it fails

The Problem

If you're working on a test that intermittently fails, it's useful to have a snippet that will run your test repeatedly until it fails.

This approach helps you reach the failing state more easily and demonstrates that the bug is likely fixed (if the test doesn't fail after 5 runs, it’s probably fixed).

While you could manually re-run your test repeatedly until it fails, what if you'd prefer to automatically restart the test when it passes?

A Solution

This shell function can be added to your .bashrc file (or whatever file your terminal program auto-loads on startup)

Without using a fixed number of times

function untilfail() {
    while $@; do :; done
}

Then, after sourcing that file (or restarting your terminal), you can use it to run your Selenium, WebdriverIO, Cypress scripts, or any type of Linux commands:

  • Example of use with Mocha
$ untilfail npx mocha --spec login-tests.js
  • Example of use with WebdriverIO
$ untilfail npx wdio --spec=login-test.js
  • Example of use with selenium
$ untilfail npm run login-test

Using a fixed number of times

Want to just run it a set number of times? Here's a different function you can use:

function run() {
    number=$1
    shift
    for i in `seq $number`; do
      $@
    done
}

To use it to run wdio 5 times, pass in the number after the command:

  • Example of use with Mocha
$ run 6 npx mocha --spec login-tests.js

Note that this second version won’t stop running if there’s a test failure. It will continue running until you either manually stop it or it completes the requested number of runs.