【发布时间】:2019-08-29 12:42:27
【问题描述】:
我的测试中有 90% 需要在开始前完成一项任务,因此我将 beforeEach 功能设置为完美运行。
剩下 10% 的人在开始之前需要做点别的事情。
除了一些测试之外,在 Cypress 中是否有一些方法可以做 beforeEach?
【问题讨论】:
标签: automation automated-tests cypress
我的测试中有 90% 需要在开始前完成一项任务,因此我将 beforeEach 功能设置为完美运行。
剩下 10% 的人在开始之前需要做点别的事情。
除了一些测试之外,在 Cypress 中是否有一些方法可以做 beforeEach?
【问题讨论】:
标签: automation automated-tests cypress
不,但你可以用它做一些技巧。例如:
describe('describe 1', function(){
beforeEach(function(){
})
it('test 1', function(){
})
it('test 2', function(){
})
})
describe('describe 2', function(){
beforeEach(function(){
})
it('test 3', function(){
})
})
这样你仍然可以将你的测试聚集在一个文件中,但是通过将它们分成几个describe(),你可以将beforeEach()分开
【讨论】:
从 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()
// ...
})
})
【讨论】: