首页 > 解决方案 > 如何在 Cypress.io 中强制测试失败

问题描述

在 Cypress.io 中,如果满足某个条件,我可以强制测试失败吗?

例子:

在我的网页上,如果字符串“对不起,出了点问题”。出现在我希望测试失败的页面上。目前这就是我正在做的事情。

/// <reference types="Cypress" />

describe("These tests are designed to fail if certain criteria are met.", () => {
  beforeEach(() => {
    cy.visit("");
  });

  specify("If 'Sorry, something went wrong.' is present on page, FAIL ", () => {
    cy.contains("Sorry, something went wrong.");
  });
});

现在,如果“对不起,出了点问题”。找到了,测试成功。如果满足此条件,我将如何通过测试?

标签: testingcypressqa

解决方案


你可以抛出一个JavaScript 异常来使测试失败:

throw new Error("test fails here")

但是,在您的情况下,我建议改用.should('not.exist')断言:

cy.contains("Sorry, something went wrong").should('not.exist')

推荐阅读