如何在 Cypress 中在失败后运行测试?

huangapple go评论74阅读模式
英文:

How run test after fail in Cypress?

问题

  1. 我在Cypress中运行了测试。
  2. 测试账户已创建。
  3. 接下来,在其中一个测试中出现了失败。
  4. 测试账户仍然保留在服务器上。
  5. 失败后,我想要删除我创建的账户。为此,我有最后一个 deleteCreatedUsers() 测试,但我不知道如何在它失败后运行它。由于测试在第一个失败的测试后被中断以节省时间,所以我需要一个在类似 after 失败后运行一个测试的解决方案。
    我尝试了 afterafterEach 和默认条件 if()。在失败后,afterafterEach 都不起作用。
英文:

Example:

  1. I ran the tests in cypress.
  2. Test accounts have been created
  3. Next I get a failure in one of the tests
  4. Test accounts remain created on the server
  5. 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();
  }
});

huangapple
  • 本文由 发表于 2023年1月9日 17:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定