【问题标题】:Stop a test if assertion failed如果断言失败则停止测试
【发布时间】:2021-04-15 11:54:31
【问题描述】:

我有一个简单的 Cypress 测试:

    describe('My First Test', () => {
      it('Go to login page', () => {
        cy.visit('http://localhost:3000')
        cy.contains('Log in').click()
      })

      it('Login with local account', () => {
        cy.get('input[type=email]').type('123@123.com')
        cy.get('input[type=password]').type('asd123')
        cy.contains('Log in').type('{enter}')
      })
    })

第一个断言检查是否存在带有文本Log in 的元素,然后单击它。第二个断言尝试登录。

我已将Log in 按钮中的文本更改为Assertion Failed。所以现在第一个断言失败了,但它仍然运行第二个断言,即使我没有被重定向到登录页面。

有没有办法在断言失败时取消正在运行的规范?

【问题讨论】:

  • 这是两种不同的测试。它只是测试的别名,如果你想让它失败,请将它们合并到一个测试源中:jestjs.io/docs/api#testname-fn-timeout
  • 以上评论为正确答案。 describe 构造将测试组合在一起。但是每个测试都应该独立。

标签: cypress


【解决方案1】:

您可以添加afterEach() 并写下:

afterEach(function() {
  if (this.currentTest.state === 'failed') {
    Cypress.runner.stop()
  }
});

或者

您可以使用插件cypress-fail-fast 并在测试级别对其进行配置:

describe("All tests", {
  failFast: {
    enabled: false, // Children tests and describes will inherit this configuration
  },
}, () => {
  it("sanity test", {
    failFast: {
      enabled: true, // Overwrite configuration defined in parents
    },
  }, () => {
    // Will skip the rest of tests if this one fails
    expect(true).to.be.true;
  });

  it("second test",() => {
    // Will continue executing tests if this one fails
    expect(true).to.be.true;
  });
});

或者,通过写cypress.json

{
  "env":
  {
    "FAIL_FAST_ENABLED": true
  }
}

【讨论】:

    【解决方案2】:

    你也可以使用

    afterEach(() => {
      if (cy.state('test').state === 'failed') {
        Cypress.runner.stop()
      }
    })
    

    但这有一个问题,你的after()钩子都不会运行,包括像代码覆盖这样的插件。

    更好的解决方案是动态跳过以下测试,类似于这个答案How to add test case grouping in Cypress

    beforeEach(function() {
      const suite = cy.state('test').parent
      if (suite.tests.some(test => test.state === 'failed')) {
        this.skip()
      }
    })
    

    这是我的简化测试

    describe('all tests', () => {
    
      describe('fail fast', () => {
    
        beforeEach(function() {               // move up to apply to all tests
          const suite = cy.state('test').parent;
          if (suite.tests.some(test => test.state === 'failed')) {
            console.log(`skipping test "${cy.state('test').title}"`)
            this.skip()
          }
        })
    
        after(() => {
          console.log('after')               // runs
        })
    
        it('fails', () => {
          expect(true).to.eq(false)          // fails
        })
    
        it('next', () => {
          expect(true).to.eq(true)           // skipped
        })
      })
    
      describe('no fail fast', () => {
        it('no skip', () => {
          expect(true).to.eq(true)           // runs
        })
      })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 2020-04-14
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多