如何在 Cypress 中添加测试用例分组

sgv*_*393 7 testng grouping ui-testing testcase cypress

我目前正在使用 Cypress 进行 UI 集成测试。我正在寻找在 cypress 中添加类似于标准 TestNG 的测试用例分组的方法。我在 cypress 文档中找不到任何分组功能。我确实找到了这篇文章:使用标签进行分组的链接。我正在寻找一种更简单的测试用例分组方法。

这是我的用例:我对不同的功能进行了测试,例如下面示例中的功能 1、2、3,每个功能都有不同的测试用例。我想对功能 1 等单个功能运行测试。有没有办法运行功能 1 的 test1。注意:我不是在寻找 .only 或 .skip。我想添加分组并使用 CLI 为特定组运行这些测试。以前有人从事过这些工作吗?


describe('Feature1', () => {
    it('test1', () => {
    })

    it('test2', () => {
    })

    it('test3', () => {
    })

})

describe('Feature2', () => {
    it('test1', () => {
    })

    it('test2', () => {
    })

    it('test3', () => {
    })
})


describe('Feature3', () => {
    it('test1', () => {
    })

    it('test2', () => {
    })

    it('test3', () => {
    })
}) 



Run Code Online (Sandbox Code Playgroud)

谢谢,萨希斯

Ric*_*sen 10

您可以skip使用 动态进行测试this.skip(),可以根据环境变量等条件有条件地应用该测试。

要全局执行此操作,请beforeEach()cypress/support/index.js中添加一个。

beforeEach(function() {

  const testFilter = Cypress.env('TEST_FILTER');
  if (!testFilter) {
    return;
  }
  
  const testName = Cypress.mocha.getRunner().test.fullTitle();
  if (!testName.includes(testFilter)) {
    this.skip();
  }
})
Run Code Online (Sandbox Code Playgroud)

请注意,您必须使用function()非箭头函数。

该变量包括来自嵌套、和 的testName文本,例如,在Cypress 提供的示例assertions.spec.js中context()describe()it()

context('Assertions', () => {
  beforeEach(() => {
    cy.visit('https://example.cypress.io/commands/assertions')
  })

  describe('Implicit Assertions', () => {
    it('.should() - make an assertion about the current subject', () => {
Run Code Online (Sandbox Code Playgroud)

testName一个

"Assertions Implicit Assertions .should() - make an assertion about the current subject"
Run Code Online (Sandbox Code Playgroud)

在 package.json 中

  "scripts": {
    "cy:open": "cypress open",
    "cy:filter:implicit": "set CYPRESS_TEST_FILTER=Implicit & cypress open"
  },
Run Code Online (Sandbox Code Playgroud)

请注意CYPRESS_前缀,但在代码中它只是TEST_FILTER

然后,

yarn cy:filter:implicit
Run Code Online (Sandbox Code Playgroud)

将跳过所有“显式断言”测试。