赛普拉斯断言 - 如何在断言后记录结果(或执行额外操作)

Cypress assertion - how to log result (or do extra action) after an assertion

我正在编写测试自动化功能。我想通过 API 将测试结果保存到我的测试计划中。 例如,我希望能够实现如下内容

      let b = expect(3).eql(4).then((result)=> {
        cy.updateTestResult(result, testCaseId)
      })

但是,断言返回的内容没有“.then()”。 我想我可能可以写一个 if/else,但我觉得它有点难看,因为我需要重复逻辑两次。 欢迎任何建议,谢谢!!

也许你可以做类似的事情

....

expect(3).to.eql(4);

// will only get here if the above passes
cy.request('POST', 'http://somehost/some-url', { some: 'json-data-here' }).then(
  (response) => {
    // ensure you got a 200 back from api call
    expect(response.status).to.eq(200);
  }
)

为了不必每次都手动执行此操作,您可以使用 afterEach,但我不确定您是否会获得有关测试本身的信息,或者是否 passed/failed

@Olore 在这里更新我的最终解决方案:

...

it('Remove VDC', () => {
    cy.delete_vdc_v2(eccData, vdc)
    .then(() => {
      expect(1).to.equal(2)
      testResult = TEST_STATUS.PASS
    })
  });

});

afterEach('Update Test Result', () => {  
  cy.updateTestResult(testResult, testRunId, testCaseId, testPointId)
});