You can dynamically skip
a test by using this.skip()
, which can be applied conditionally based on, say, an environment variable.
To do it globally add a beforeEach()
in 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();
}
})
Note, you must use a function()
not an arrow function.
The variable testName
includes the text from nested context()
, describe()
, and it()
, for example, in the sample assertions.spec.js provided by Cypress
This
context('Assertions', () => {
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/assertions')
})
describe('Implicit Assertions', () => {
it('.should() - make an assertion about the current subject', () => {
has a testName
of
"Assertions Implicit Assertions .should() - make an assertion about the current subject"
In package.json
"scripts": {
"cy:open": "cypress open",
"cy:filter:implicit": "set CYPRESS_TEST_FILTER=Implicit & cypress open"
},
Note the CYPRESS_ prefix, but in the code it's just TEST_FILTER.
Then,
yarn cy:filter:implicit
will skip all the "Explicit Assertions" tests.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…