cypress - 如果断言失败则停止测试

标签 cypress

我有一个简单的 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。所以现在第一个断言失败了,但它仍然运行第二个断言,即使我没有被重定向到登录页面。

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

最佳答案

您可以添加一个 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
  }
}

关于cypress - 如果断言失败则停止测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67107973/

相关文章:

cypress - 如何使用 Cypress 获得同一类的第二个 div

cypress - cy.visit() 失败,因为在单个测试用例中尝试访问第二个唯一域/位置/url 时

cypress - 使用 "this"在 Cypress 中访问别名

testing - Cypress :如何在测试的所有测试开始之前运行一次代码

automated-tests - 使用 Cypress 定期检索和比较元素的样式属性

javascript - 如何访问 Cypress 的 React 组件

cypress - 未找到 "match.text"链接器

mocha.js - 在 cypress 中每次测试后获取测试状态

typescript - Cypress 断言文本,同时忽略软连字符

javascript - 在 Cypress 的片状测试中寻找 cy.wait() 的替代品