一旦测试失败,是否有可靠的方法可以使赛普拉斯退出? [英] Is there a reliable way to have Cypress exit as soon as a test fails?
问题描述
我们在CI服务器上运行着一个大型测试套件,并且似乎无法告诉Cypress如果测试失败则退出。它总是运行整个套件。
We have a large test suite running on a CI server, and there appears to be no way of telling Cypress to exit if a test fails. It always runs the entire suite.
有一些讨论此处,但没有可行的解决方案。
There is some discussion here, but no workable solution.
有没有一种可靠的方法可以使Cypress在测试失败后立即退出?
Is there a reliable way to have Cypress exit as soon as a test fails?
推荐答案
正如您所提到的,它尚未得到正式支持(从3.6.0版开始)。
As you've mentioned, it's not officially supported yet (as of 3.6.0).
这是我的黑客手段(不使用使用cookie等保持状态):
Here's my take at a hack (without the use of cookies and such for keeping state):
// cypress/plugins/index.js
let shouldSkip = false;
module.exports = ( on ) => {
on('task', {
resetShouldSkipFlag () {
shouldSkip = false;
return null;
},
shouldSkip ( value ) {
if ( value != null ) shouldSkip = value;
return shouldSkip;
}
});
}
// cypress/support/index.js
function abortEarly () {
if ( this.currentTest.state === 'failed' ) {
return cy.task('shouldSkip', true);
}
cy.task('shouldSkip').then( value => {
if ( value ) this.skip();
});
}
beforeEach(abortEarly);
afterEach(abortEarly);
before(() => {
if ( Cypress.browser.isHeaded ) {
// Reset the shouldSkip flag at the start of a run, so that it
// doesn't carry over into subsequent runs.
// Do this only for headed runs because in headless runs,
// the `before` hook is executed for each spec file.
cy.task('resetShouldSkipFlag');
}
});
一旦遇到故障,将跳过所有进一步的测试。输出结果如下:
Will skip all further tests once a failure is encountered. The output will look like:
< img src = https://i.stack.imgur.com/sStHn.png alt =在此处输入图片描述>
这篇关于一旦测试失败,是否有可靠的方法可以使赛普拉斯退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!