Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retry a failed test?

I'll sometimes have 1 or 2 tests that fail in CI, and rerunning the build causes them to pass.

How can I automatically re-run these flaky tests so my build will pass the first time? Is there something similar to mocha's this.retries?

For example, I have a test that fails with "The element has an effective height of 0x0" about 10% of the time:

cy.visit('/')
cy.get('.my-element').click() // sometimes fails with not visible error
like image 330
bkucera Avatar asked Feb 13 '19 15:02

bkucera


People also ask

How do you rerun a failed test case?

To rerun failed test runs automatically during the test run itself, we implement IRetryAnalyzer interface provided by TestNG. By overriding retry() method of the interface in your class, you can control the number of attempts to rerun a failed test case.

How do you run a failed test?

If you want to execute only failed test cases through the Eclipse, then first refresh the project. Step 1) Right click on the java project (Demo A and B). Select the refresh option or simply select the java project and press F5. Step 2) Then you will able to see the test-output folder.

How do I retry in selenium?

For the first retry, if the method passes, the method's execution ends with the status PASSED. If the method fails again, the method's status is set to SKIPPED and the method is retried (retry count = 1). For the second retry, if the method passes, the method's ends with the status PASSED.

Which interface is used to retry the failed test cases in TestNG?

In this article, we will learn how to retry failed test in TestNG with IRetryAnalyzer and also how to rerun all tests from scratch in a test class. To retry a failed test, we will use the IRetryAnalyzer interface. It reruns the Selenium TestNG tests when they are failed.


2 Answers

Update (v5.0.0)

Cypress now has built-in retry support.

You can set test retries in Cypress 5.0 via configuration in cypress.json

{
  "retries": 1
}

or specify different options for runMode and openMode:

{
  "retries": {
    "runMode": 1,
    "openMode": 3
  }
}

runMode allows you to define the number of test retries when running cypress run

openMode allows you to define the number of test retries when running cypress open

You can turn on test retries for just a single test or suite via test options:

it('my test', {
  retries: 2
}, () => {
  // ...
})

// or

describe('my suite', {
  retries: 2
}, () => {
  // ...
})

If a test fails in a beforeEach, afterEach, or in the test body, it will be retried. Failures in beforeAll and afterAll hooks will not retry.


Old answer:

Official Support for test retries is on the way, but there's a plugin for that. cypress-plugin-retries

Disclosure: I'm the creator of the plugin.

Installation

Add the plugin to devDependencies

npm install -D cypress-plugin-retries

At the top of cypress/support/index.js:

require('cypress-plugin-retries')

Usage

Use the environment variable CYPRESS_RETRIES to set the retry number:

CYPRESS_RETRIES=2 npm run cypress

or use Cypress.env('RETRIES') in your spec file:

Cypress.env('RETRIES', 2)

or on a per-test or per-hook basis, set the retry number:

Note: this plugin adds Cypress.currentTest and you should only access it in the context of this plugin.

it('test', () => {
    Cypress.currentTest.retries(2)
})

Note: Please refer to this issue for updates about official cypress retry support

like image 131
bkucera Avatar answered Oct 06 '22 07:10

bkucera


Cypress 5 now has a native support for retries. Check: https://cypress.io/blog/2020/08/19/introducing-test-retries-in-cypress-5-0

like image 31
Mohamed Hamed Avatar answered Oct 06 '22 05:10

Mohamed Hamed