我有一个简单的 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/