Retry Flaky Tests
You can rerun certain tests with the WebdriverIO testrunner that turn out to be unstable due to things like a flaky network or race conditions. (However, it is not recommended to simply increase the rerun rate if tests become unstable!)
#
Rerun suites in MochaSince version 3 of Mocha, you can rerun whole test suites (everything inside an describe
block). If you use Mocha you should favor this retry mechanism instead of the WebdriverIO implementation that only allows you to rerun certain test blocks (everything within an it
block). In order to use the this.retries()
method, the suite block describe
must use an unbound function function(){}
instead of a fat arrow function () => {}
, as described in Mocha docs. Using Mocha you can also set a retry count for all specs using mochaOpts.retries
in your wdio.conf.js
.
Here is an example:
#
Rerun single tests in Jasmine or MochaTo rerun a certain test block you can just apply the number of reruns as last parameter after the test block function:
The same works for hooks too:
If you are using Jasmine, it also means that second parameter of both test functions (e.g., it
) and hooks (e.g., beforeEach
) , which is a timeout
in Jasmine, is treated as retry count.
It is not possible to rerun whole suites with Jasmine—only hooks or test blocks.
#
Rerunning in Cucumber#
Rerun full suites in CucumberFor cucumber >=6 you can provide the retry
configuration option along with a retryTagFilter
optional parameter to have all or some of your failing scenarios get additional retries until succeeded. For this feature to work you need to set the scenarioLevelReporter
to true
.
#
Rerun Step Definitions in CucumberTo define a rerun rate for a certain step definitions just apply a retry option to it, like:
Reruns can only be defined in your step definitions file, never in your feature file.
#
Add retries on a per-specfile basisPreviously, only test- and suite-level retries were available, which are fine in most cases.
But in any tests which involve state (such as on a server or in a database) the state may be left invalid after the first test failure. Any subsequent retries may have no chance of passing, due to the invalid state they would start with.
A new browser
instance is created for each specfile, which makes this an ideal place to hook and setup any other states (server, databases). Retries on this level mean that the whole setup process will simply be repeated, just as if it were for a new specfile.