Cypress - 从 beforeEach 中排除测试

Dom*_*ála 5 automation automated-tests cypress

我 90% 的测试需要在开始前完成一项任务,所以我制作了 beforeEach 完美运行的功能。

剩下的 10% 需要在开始之前做些别的事情。

除了一些测试之外,在赛普拉斯中是否有一些方法可以在 beforeEach 中进行?

Mr.*_* J. 8

不,但你可以用它做一些技巧。例如:

describe('describe 1', function(){
  beforeEach(function(){
  })
  it('test 1', function(){
  })
  it('test 2', function(){
  })
})

describe('describe 2', function(){
  beforeEach(function(){
  })
  it('test 3', function(){
  })
})
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您仍然可以将测试聚集在 1 个文件中,但是通过将它们describe()分成几个's,您可以将beforeEach()


小智 7

从 cypress 8.2.0 及更高版本开始,您可以使用该Cypress.currentTest对象来检查每次正在运行哪个测试。

describe('describe 1', () => {

    beforeEach(() => {
        switch(Cypress.currentTest.title) {
             case 'test 3 - i am not so usual':
             // case 'test 4 - not so usual too': (or any other test title)
                  cy.yourCustomCommand()
                  // or let your special test to take control...below
             break;
             default:
                 // do as usual
                 cy.yourStandardCommand()
             break;
        }
    })
    
     it('usual test 1', () => {})
    
     it('usual test 2', () => {})

     it('test 3 - i am not so usual', () => {
          cy.letMeDoMyStaff()
          // ...
     })
})
Run Code Online (Sandbox Code Playgroud)

文档: https: //docs.cypress.io/api/cypress-api/currenttest