【问题标题】:Is there a reliable way to have Cypress exit as soon as a test fails?一旦测试失败,是否有可靠的方法让赛普拉斯退出?
【发布时间】:2019-11-01 10:46:33
【问题描述】:

我们在 CI 服务器上运行了一个大型测试套件,如果测试失败,似乎无法告诉赛普拉斯退出。它始终运行整个套件。

有一些讨论here,但没有可行的解决方案。

是否有可靠的方法让赛普拉斯在测试失败后立即退出?

【问题讨论】:

    标签: javascript testing continuous-integration cypress


    【解决方案1】:

    您也可以使用这个 Cypress 插件,同时 Cypress 本身不支持此功能:https://www.npmjs.com/package/cypress-fail-fast

    将插件添加到 devDependencies:

    npm i --save-dev cypress-fail-fast
    

    在 cypress/plugins/index.js 内部:

    module.exports = (on, config) => {
      require("cypress-fail-fast/plugin")(on, config);
      return config;
    };
    

    在 cypress/support/index.js 的顶部:

    import "cypress-fail-fast";
    

    【讨论】:

    • 它不会完全停止 cypress,它只会中止一个测试文件
    • 嗨@AlexandrZavalii,这不是插件的预期行为。来自its documentation:“如果一个测试在最后一次重试后失败,其余的测试将被跳过”,还有一个屏幕截图显示它如何跳过其余文件中的测试。如果插件不适合您,也许您应该在插件存储库中open an issue。 ;)
    【解决方案2】:

    正如您所提到的,它尚未得到官方支持(截至 3.6.0)。

    这是我对 hack 的看法(使用 cookie 等来保持状态):

    // 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');
      }
    });
    

    一旦遇到故障,将跳过所有进一步的测试。输出将如下所示:

    【讨论】:

    • 这是一个很好的解决方案,但绝对应该支持开箱即用。
    • 同意。无论如何,即使这是一个丑陋的黑客攻击。
    • 大家好!你知道停止运行其他测试文件的方法吗?所以一个失败应该停止运行测试的整个过程
    猜你喜欢
    • 2021-11-14
    • 2020-08-05
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2019-10-24
    • 2023-02-14
    • 2020-08-22
    • 1970-01-01
    相关资源
    最近更新 更多