一种方法是使用Cypress-Select-Tests 插件。
1.使用npm install --save-dev cypress-select-tests安装插件
2.安装后,写在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 和 cypress-examples-recipes grep。