如何在赛普拉斯中添加测试用例分组

How to add test case grouping in Cypress

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

这是我的用例:我在下面的示例中测试了不同的功能,例如 feature1、2、3,每个功能都有不同的测试用例。我想 运行 我对功能 1 等个别功能的测试。有没有办法 运行 测试功能 1 的 1。注意:我不是在寻找 .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', () => {
    })
}) 



谢谢, 萨希斯

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

要全局执行,请在 cypress/support/index.js.

中添加一个 beforeEach()
beforeEach(function() {

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

注意,您必须使用 function() 而不是箭头函数。

变量 testName 包含嵌套的 context()describe()it() 中的文本,例如示例 assertions.spec.js 由赛普拉斯提供

这个

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

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

testName

"Assertions Implicit Assertions .should() - make an assertion about the current subject"

在package.json

  "scripts": {
    "cy:open": "cypress open",
    "cy:filter:implicit": "set CYPRESS_TEST_FILTER=Implicit & cypress open"
  },

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

然后,

yarn cy:filter:implicit

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

一种方法是使用 Cypress-Select-Tests 插件。

1.Install 插件使用 npm install --save-dev cypress-select-tests

2.Once安装,cypress/plugins/index.js下写:

const selectTestsWithGrep = require('cypress-select-tests/grep')
module.exports = (on, config) => {
  on('file:preprocessor', selectTestsWithGrep(config))
}

现在根据您的要求,您可以执行如下测试:

 ## run tests with "works" in their full titles 
 $ npx cypress open --env grep=works

 ## runs only specs with "foo" in their filename 
 $ npx cypress run --env fgrep=foo

 ## runs only tests with "works" from specs with "foo" 
 $ npx cypress run --env fgrep=foo,grep=works

 ## runs tests with "feature A" in the title 
 $ npx cypress run --env grep='feature A'

 ## runs only specs NOT with "foo" in their filename 
 $ npx cypress run --env fgrep=foo,invert=true

 ## runs tests NOT with "feature A" in the title 
 $ npx cypress run --env grep='feature A',invert=true

现在,如果您想编写自己的自定义逻辑来过滤掉测试,您也可以这样做。在您的 cypress/plugins/index.js 中使用此模块作为文件预处理器并编写您自己的 pickTests 函数。

const selectTests = require('cypress-select-tests')

// return test names you want to run
const pickTests = (filename, foundTests, cypressConfig) => {

  // found tests will be names of the tests found in "filename" spec
  // it is a list of names, each name an Array of strings
  // ['suite 1', 'suite 2', ..., 'test name']

  // return [] to skip ALL tests
  // OR
  // let's only run tests with "does" in the title

  return foundTests.filter(fullTestName => fullTestName.join(' ').includes('does'))
}

module.exports = (on, config) => {
  on('file:preprocessor', selectTests(config, pickTests))
}

您也可以参考这些示例以供进一步参考:cypress-select-tests-example and cypress-examples-recipes grep