我已经配置了npm (npm软断言)的软断言,现在我的package.josn有了“软断言”:"^0.2.3“。
我想使用软断言的功能。
softAssert(actual, expected, msg, ignoreKeys)但不知道,具体的步骤是什么
示例:当我在代码中使用软断言函数时,会出现以下错误。
如果我像这样用
有谁能告诉我如何在柏树代码中使用这个'softAssert‘函数?
现在我面临的问题
it('asserts and logs and fails', () => {
Cypress.softAssert(10, 12, "expected actual mismatch...");
cy.log("text")
Cypress.softAssertAll();
}) 我需要将软断言后的代码作为cy.log("text")在相同的'it‘块中执行,但是当前的测试失败了整个'it’块,而不执行‘cy.log(“文本”)’语句。
发布于 2021-02-10 08:17:21
软断言概念非常酷,您可以用最少的实现将它添加到Cypress中。
const jsonAssertion = require("soft-assert")
it('asserts several times and only fails at the end', () => {
jsonAssertion.softAssert(10, 12, "expected actual mismatch");
// some more assertions, not causing a failure
jsonAssertion.softAssertAll(); // Now fail the test if above fails
})对我来说,在日志中看到每个软断言失败会更好,因此可以添加自定义命令来包装软断言函数。
const jsonAssertion = require("soft-assert")
Cypress.Commands.add('softAssert', (actual, expected, message) => {
jsonAssertion.softAssert(actual, expected, message)
if (jsonAssertion.jsonDiffArray.length) {
jsonAssertion.jsonDiffArray.forEach(diff => {
const log = Cypress.log({
name: 'Soft assertion error',
displayName: 'softAssert',
message: diff.error.message
})
})
}
});
Cypress.Commands.add('softAssertAll', () => jsonAssertion.softAssertAll())
//-- all above can go into /cypress/support/index.js
//-- to save adding it to every test (runs once each test session)
it('asserts and logs but does not fail', () => {
cy.softAssert(10, 12, "expected actual mismatch...");
cy.log('text'); // this will run
})
it('asserts and logs and fails', () => {
cy.softAssert(10, 12, "expected actual mismatch...");
cy.log('text'); // this will run
cy.softAssertAll();
})https://stackoverflow.com/questions/66132293
复制相似问题