我有两个测试套件(我使用的是摩卡的TDD UI,它使用suite()
,test()而不是describe()
和it()
):
suite('first suite'), function(){
....
})
suite('second suite', function(){
beforeEach(function(done){
console.log('I SHOULD NOT BE RUN')
this.timeout(5 * 1000);
deleteTestAccount(ordering, function(err){
done(err)
})
})
...
}()
运行mocha -g 'first suite
仅从第一个套件运行测试,但运行 beforeEach,在控制台上打印I SHOULD NOT BE RUN
。
如何使beforeEach()
仅在其中包含的套件中运行?
注意:我可以通过以下方式解决此问题:
beforeEach(function(done){
this.timeout(5 * 1000);
if ( this.currentTest.fullTitle().includes('second suite') ) {
deleteTestAccount(ordering, function(err){
done(err)
})
} else {
done(null)
}
})
问题是beforeEach
不是TDD UI的一部分,而是BDD UI。TDD UI 对应的函数是 setup
。因此,请尝试用setup
替换beforeEach
,一切都应该按您的预期:)工作。