【问题标题】:Run specific part of Cypress test multiple times (not whole test)多次运行赛普拉斯测试的特定部分(不是整个测试)
【发布时间】:2021-11-12 14:02:08
【问题描述】:

是否可以在 Cypress 中一遍又一遍地运行测试的特定部分而不执行整个测试用例?我在测试用例的第二部分出错,前半部分需要 100 秒。这意味着我每次都必须等待 100 秒才能到达发生错误的地步。我想在错误发生前几步重新运行测试用例。所以再一次,我的问题是:有可能在赛普拉斯做吗?谢谢

【问题讨论】:

  • 你的意思是像 1 个 it 块内的某些部分,你的意思是有多个 it 块,你只想运行一些特定的 it 块吗?
  • 抱歉,不是很清楚。因为我用的是黄瓜+柏树,所以不知道怎么形容。考虑 1 个 Given() 块,10 个 And() 块,在第 8 个 And() 块中出现错误。我只想重新运行第 7 个块并继续,而不是再次从 1 个给定块开始。你懂我的意思吗? @AlapanDas
  • 任何代码行都可以使用迭代语句运行任意多次。顺便说一句,如果您愿意共享一些上下文(您的测试代码),那么提供解决方案会容易得多。

标签: cypress cypress-cucumber-preprocessor


【解决方案1】:

解决方法 #1

如果您在 cypress 中使用 cucumber,您可以将您的场景修改为一个场景大纲,该大纲将使用场景标签执行 N 次:

@runMe
Scenario Outline: Visit Google Page
  Given that google page is displayed
  Examples:
   | nthRun |
   | 1  |
   | 2  |
   | 3  |
   | 4  |
   | 100  |

然后通过标签运行在终端中运行测试:

./node_modules/.bin/cypress-tags run -e TAGS='@runMe'

参考:https://www.npmjs.com/package/cypress-cucumber-preprocessor?activeTab=versions#running-tagged-tests

解决方法 #2

Cypress 确实具有重试功能,但它只会在失败时重试该场景。您可以使用场景标签强制您的场景重试 N 次失败:

在您的 cypress.json 中添加以下配置:

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

参考:https://docs.cypress.io/guides/guides/test-retries#How-It-Works

下一步是在您的功能文件中,在您的场景的最后一步添加未知步骤定义以使其失败:

@runMe
Scenario: Visit Google Page
  Given that google page is displayed
  And I am an uknown step

然后通过标签运行测试:

./node_modules/.bin/cypress-tags run -e TAGS='@runMe'

【讨论】:

    【解决方案2】:

    对于不需要向配置文件添加更改的解决方案,您可以将重试作为参数传递给特定测试,这些测试由于可接受的原因而被认为是易碎的。

    https://docs.cypress.io/guides/guides/test-retries#Custom-Configurations

    你可以写的意思(来自文档)

    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
      • 2023-02-14
      • 2021-10-05
      • 2019-12-27
      • 2019-07-29
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多