【问题标题】:How can I retry a failed test?如何重试失败的测试?
【发布时间】:2019-02-13 15:45:39
【问题描述】:

我有时会有 1 或 2 个测试在 CI 中失败,重新运行构建会使它们通过。

如何自动重新运行这些不稳定的测试,以便我的构建第一次通过?有没有类似mochathis.retries的东西?

例如,我有一个测试在大约 10% 的时间以“元素的有效高度为 0x0”失败:

cy.visit('/')
cy.get('.my-element').click() // sometimes fails with not visible error

【问题讨论】:

  • 好点,这个很有用
  • @bkucera 在问题中显示具体场景可能很有用。你有围绕插件的单元测试吗?否则这更像是一个广告而不是一个 SO 问题
  • @eric99 这是我们在 gitter 中经常遇到的一个问题,并且有一个 GitHub 问题要求该功能。当柏树用户经常提出问题时,我会在此处交叉发布问答,但我同意一个示例会有所帮助
  • 那么你是如何测试这个插件的?
  • 我只是希望你能提出一个更实质性的问题以供将来参考。

标签: continuous-integration cypress


【解决方案1】:

更新 (v5.0.0)

赛普拉斯现在具有内置的重试支持。

您可以通过cypress.json 中的配置在 Cypress 5.0 中设置测试重试

{
  "retries": 1
}

或为 runMode 和 openMode 指定不同的选项:

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

runMode 允许您在运行 cypress run 时定义测试重试次数

openMode 允许您定义运行 cypress open 时的测试重试次数

您可以通过测试选项为单个测试或套件打开测试重试:

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

// or

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

如果测试在 beforeEachafterEach 或测试正文中失败,则会重试。 beforeAll 和 afterAll 钩子中的失败不会重试。


旧答案:

对测试重试的官方支持正在进行中,但有一个插件可以做到这一点。 cypress-plugin-retries

披露:我是插件的创建者。

安装

将插件添加到 devDependencies

npm install -D cypress-plugin-retries

cypress/support/index.js的顶部:

require('cypress-plugin-retries')

用法

使用环境变量CYPRESS_RETRIES设置重试次数:

CYPRESS_RETRIES=2 npm run cypress

在您的规范文件中使用Cypress.env('RETRIES')

Cypress.env('RETRIES', 2)

在每个测试或每个钩子的基础上,设置重试次数:

注意:此插件添加了 Cypress.currentTest,您只能在此插件的上下文中访问它。

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

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

【讨论】:

  • 它也适用于柏木黄瓜吗?我在cypress.json 中添加了env: { "retries": 2} 并在/support/index.js 中导入'cypress-plugin-retries';,但我没有看到任何重试发生。请问有什么想法吗?
  • 你需要大写的'RETRIES'
  • 是的,解决了。当我使用打字稿时,我还必须将cypress-plugin-retries 添加到类型中。
  • @bkucera 我们可以只对所需的失败测试用例进行重试吗?
  • Cypress.currentTest.retries(3)
【解决方案2】:

【讨论】:

    【解决方案3】:

    要重试,您可以将其添加到您的配置中,如下所示

    {
      "retries": {
        // Configure retry attempts for `cypress run`
        // Default is 0
        "runMode": 2,
        // Configure retry attempts for `cypress open`
        // Default is 0
        "openMode": 0
      }
    }
    

    但不是在上面添加,我更喜欢添加(适用于运行和打开模式)

    “重试次数”:1

    需要考虑的要点

    1. 你需要cypress版本> 5
    2. 到目前为止,在 cypress 中,您可以重试测试,而不是整个规范,这将是使测试更加健壮的强大功能。我已经将其投票为关键功能(也是目前投票最多的功能之一) 参考: https://portal.productboard.com/cypress-io/1-cypress-dashboard/tabs/1-under-consideration

    【讨论】:

      【解决方案4】:

      赛普拉斯从 2020 年 8 月 19 日发布的版本 5.0.0 开始支持测试重试。这些是为了减少测试不稳定和持续集成构建失败。此功能记录在Test Retries 指南下的在线赛普拉斯文档中。

      默认情况下,测试失败时不会重试。要重试失败的测试,测试重试需要enabled in the configuration

      可以为运行模式 (cypress run) 和开放模式 (cypress open) 分别配置重试,因为它们通常会有所不同。

      有两种方法可以配置重试:全局和每个测试或测试套件。

      Global Configuration

      赛普拉斯configuration file(默认为cypress.json)允许为每个模式或所有模式配置重试尝试。

      要为每个模式使用不同的值:

      {
        "retries": {
          "runMode": 2,
          "openMode": 0
        }
      }
      

      要对两种模式使用相同的值:

      {
        "retries": 1
      }
      

      Single test configuration

      test's configuration 可以指定特定于该测试的重试次数:

      // Customize retry attempts for an individual test
      describe('User sign-up and login', () => {
        // `it` test block with no custom configuration
        it('should redirect unauthenticated user to sign-in page', () => {
          // ...
        })
      
        // `it` test block with custom configuration
        it(
          'allows user to login',
          {
            retries: {
              runMode: 2,
              openMode: 1,
            },
          },
          () => {
            // ...
          }
        )
      })
      

      Single test suite configuration

      测试套件的配置可以指定该套件中每个测试的重试次数:

      // Customizing retry attempts for a suite of tests
      describe('User bank accounts', {
        retries: {
          runMode: 2,
          openMode: 1,
        }
      }, () => {
        // The per-suite configuration is applied to each test
        // If a test fails, it will be retried
        it('allows a user to view their transactions', () => {
          // ...
        }
      
        it('allows a user to edit their transactions', () => {
          // ...
        }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多