get test status after each test in cypress

2.4k views Asked by At

I am looking for an option to get the name and the result (fail/pass) of each test to save this to an external file.

How can I get this information easily?

Currently, I can get only the title within the it() section with:

cy.log(this.test.title)

however, I am not able to get the result there.

It would be ideal to have this option from afterEach() section. So that in one place I can save the title and result.

I cannot do this.test.state because Cannot read property 'test' of undefined

I am not able to use Cypress.on('test:after:run', (test)) because this part of code is not starting. I have no idea why.

Thanks!

1

There are 1 answers

0
Sudheer Singh On

Use the below snippet, it will work:

Cypress.mocha.getRunner().suite.ctx.currentTest.state 
  • it will tell you status as passed or failed
  • You only need to integrate mocha plugin with the cypress project.

You can the above scripts like shown below:

if (Cypress.mocha.getRunner().suite.ctx.currentTest.state === 'failed') {
            cy.log('Failed')
        } else {
            cy.log('Passed')
 }