Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cypress 如果断言失败,则停止测试_Cypress - Fatal编程技术网

Cypress 如果断言失败,则停止测试

Cypress 如果断言失败,则停止测试,cypress,Cypress,我有一个简单的Cypress测试: description('我的第一次测试',()=>{ 它('转到登录页',()=>{ 参观http://localhost:3000') cy.contains('登录')。单击() }) 它('使用本地帐户登录',()=>{ cy.get('input[type=email]')。type('123@123.com') cy.get('input[type=password]')。type('asd123') cy.contains('Log in')。类

我有一个简单的Cypress测试:

description('我的第一次测试',()=>{
它('转到登录页',()=>{
参观http://localhost:3000')
cy.contains('登录')。单击()
})
它('使用本地帐户登录',()=>{
cy.get('input[type=email]')。type('123@123.com')
cy.get('input[type=password]')。type('asd123')
cy.contains('Log in')。类型(“{enter}”)
})
})
第一个断言检查是否有带有文本
Log in
的元素,然后单击它。第二个断言尝试登录

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


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

您可以在each()之后添加一个
并编写以下内容:

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

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

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
  }
}
你也可以使用

每次(()=>{ 如果(cy.state('test')。state=='failed'){ 赛普拉斯。跑步者。停止() } }) 但是这有一个问题,您的
after()
钩子都不会运行,包括像代码覆盖率这样的插件

一个更好的解决方案是动态跳过以下测试,类似于这个答案

beforeach(函数(){
const suite=cy.state('test')。父级
if(suite.tests.some(test=>test.state=='failed')){
this.skip()
}
})
这是我的简化测试

description('所有测试',()=>{
描述('fail fast',()=>{
beforeach(function(){//上移以应用于所有测试
const suite=cy.state('test')。父级;
if(suite.tests.some(test=>test.state=='failed')){
log(`skippingtest“${cy.state('test').title}`)
this.skip()
}
})
之后(()=>{
console.log('after')//运行
})
它('失败',()=>{
expect(true).to.eq(false)//失败
})
它('next',()=>{
expect(true).to.eq(true)//跳过
})
})
描述('无故障快速',()=>{
它('不跳过',()=>{
expect(true).to.eq(true)//运行
})
})
})

这是两种不同的测试。它只是测试的别名,如果您希望它失败,请将它们合并到一个测试源中:上面的注释是正确的答案。描述构造将测试分组在一起。但每项测试都应该是独立的。