英文:
How run test after fail in Cypress?
问题
- 我在Cypress中运行了测试。
- 测试账户已创建。
- 接下来,在其中一个测试中出现了失败。
- 测试账户仍然保留在服务器上。
- 失败后,我想要删除我创建的账户。为此,我有最后一个
deleteCreatedUsers()
测试,但我不知道如何在它失败后运行它。由于测试在第一个失败的测试后被中断以节省时间,所以我需要一个在类似after
失败后运行一个测试的解决方案。
我尝试了after
、afterEach
和默认条件if()
。在失败后,after
和afterEach
都不起作用。
英文:
Example:
- I ran the tests in cypress.
- Test accounts have been created
- Next I get a failure in one of the tests
- Test accounts remain created on the server
- After failure, I want to delete the accounts I created. For it, I have the last
deleteCreatedUsers()
test, but I don't know how to run it after it fails. Since the tests are interrupted after the first failed test to save time.
I need a solution to run one test after something like after
fails
I did try after
, afterEach
and default condition if()
. After fail after
, afterEach
don't do anything.
答案1
得分: 3
有另一个地方可以放置您的代码,如果您发现afterEach()
在测试失败时不运行。
Cypress.on('fail', (error, runner) => {
deleteCreatedUsers();
})
Cypress推荐的方法是在beforeEach()
中执行清理,如前所述,但上述事件侦听器也可能会根据您在deleteCreatedUsers()
内部的操作而起作用。
英文:
There is another place to put your code if you find afterEach()
does not run when the test fails.
Cypress.on('fail', (error, runner) => {
deleteCreatedUsers();
})
The recommended approach from Cypress is to perform cleanup in beforeEach()
as already mentioned, but the above event listener may also work depending on what you are doing inside deleteCreatedUsers()
.
答案2
得分: 2
Also try the After Run API. It runs in the Node process of Cypress, set up in cypress.config.js
.
It should always run after test run, pass of fail. But your deleteCreatedUsers()
sounds maybe like browser-code, you will need to adapt it for the Node process.
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:run', (results) => {
deleteCreatedUsers()
})
}
}
})
It is also possible to run in beforeEach()
instead of afterEach()
, that way the tests are always starting with a clear users situation.
beforeEach(() => {
deleteCreatedUsers()
})
英文:
Also try the After Run API. It runs in the Node process of Cypress, set up in cypress.config.js
.
It should always run after test run, pass of fail. But your deleteCreatedUsers()
sounds maybe like browser-code, you will need to adapt it for the Node process.
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:run', (results) => {
deleteCreatedUsers()
})
}
}
})
It is also possible to run in beforeEach()
instead of afterEach()
, that way the tests are always starting with a clear users situation.
beforeEach(() => {
deleteCreatedUsers()
})
答案3
得分: 0
你可以创建一个包含此处的代码的afterEach
块,并使用测试的状态来确定是否执行您的代码。
afterEach(() => {
if (Cypress.mocha.getRunner().suite.ctx.currentTest.state === 'failed') {
deleteCreatedUsers();
}
});
英文:
You can create an afterEach
block that contains the code here, and use the status of the test to determine whether or not to execute your code.
afterEach(() => {
if (Cypress.mocha.getRunner().suite.ctx.currentTest.state === 'failed') {
deleteCreatedUsers();
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论