在相同的 Cucumberjs 定义中,对于赛普拉斯中的状态 (Given) 和结果 (Then),哪个是最佳实践?

Which is the best practice in identical Cucumberjs definitions for a state (Given) and an outcome (Then), in Cypress?


对某些步骤定义使用 Cucumber.jsCypress 的最佳实践有一些疑问。

基本上,测试需要一个可以相同实现的状态和结果:

这是状态的例子:

Given(
  'the {int} item of the {string} component is visible',
  (index, ComponentIndex, mapper: ComponentMapper) => {
    cy.get('[data-test^="cmp-"]').eq(cmpMap[mapper]).as(mapper)
    cy.get(`@${mapper}`)
      .find('.component__item')
      .eq(index)
      .should('be.visible')
  }
)

这是结果示例:

Then(
  'the {int} item of the {string} component is visible',
  (index, ComponentIndex, mapper: ComponentMapper) => {
    cy.get('[data-test^="cmp-"]').eq(cmpMap[mapper]).as(mapper)
    cy.get(`@${mapper}`)
      .find('.component__item')
      .eq(index)
      .should('be.visible')
  }
)

在这种情况下,我只看到代码重复,因为 GivenThen 实现相同的代码,执行相同的检查。我想了解所有可能的解决方案,以最有效的方式实现这一目标。

双重实施

保留描述中定义的双步骤
专业版:更清晰
缺点:双码\

单一实施

留下一个实现,在Given或者Then,因为cucumber其实不检查特征文件中Step和step中相关函数的匹配定义文件
PRO:代码的单一实现
缺点:步骤定义不太清楚且更混乱

外部函数

创建一个函数以放入例如赛普拉斯的某些命令或实用程序中,并在两个定义中使用它
PRO:单一实施
缺点:过度设计?在 Cucumber.js

中从未见过这种方法

全部检查一遍,你会推荐哪一个?谢谢

还有一个选项,基本上是“Single implementation”,但更清晰一点。我假设你使用的是 cypress-cucumber-preprocessor,除了 Given/When/Then,你还可以导入 defineStep 做完全相同的事情,但是从代码的角度来看更清楚的是,以这种方式定义的步骤是通用的。

import { defineStep } from "cypress-cucumber-preprocessor/steps";

defineStep('the {int} item of the {string} component is visible', 
    (index, ComponentIndex, mapper: ComponentMapper) => {
    ...
});