[英]Trying to skip test case based on condition in a loop
I have the following test code, my intention is when looping thru objects I want to check the condition and run for first time and assign go_on
value to false
and skip tests when false. 我有以下测试代码,我的意图是在循环遍历要检查条件并第一次运行的对象时,将
go_on
值分配为false
并在为false时跳过测试。 I tested this code without looping thru the object and it works. 我在不循环遍历对象的情况下测试了此代码,它可以正常工作。 I use nodejs and mocha test framework.
我使用nodejs和mocha测试框架。
var s={a:c,b:a};
Object.keys(s).forEach(function (x){
before(function( done1 ){
if (go_on==true) {
done1();
}
else this.skip();
});
it('Exit if go_on false', function (done) {
go_on=false;
console.log(go_on);
done();
});
});
It seems like you are setting go_on=false
in your it
, but then using it in your before
. 好像你正在设置
go_on=false
在你it
,但随后在你使用它before
。 before
code occurs before it
code, so the value of go_on
set in the it
will never get used. before
的代码发生之前it
的代码,这样的价值go_on
组在it
永远不会习惯。
Also, you're not actually using s
or x
anywhere. 另外,您实际上并没有在任何地方使用
s
或x
。
If I understand correctly, you are trying to skip some tests based on data conditions. 如果我理解正确,则您正在尝试根据数据条件跳过某些测试。 You have the basic idea correct, but you need to use
beforeEach()
versus before()
. 您的基本概念正确,但是需要使用
beforeEach()
和before()
。 The before()
callback executes before the entire test suite whereas the beforeEach()
callback runs before EACH test. before()
回调在整个测试套件之前执行,而beforeEach()
回调在beforeEach()
测试之前运行。 Here is an example that works: 这是一个有效的示例:
describe('conditional tests', function() {
var go_on = true;
var s={ a:'1', b:'2', c:'3'};
Object.keys(s).forEach(function (x){
beforeEach(function( done ){
if (go_on === true) {
done();
}
else this.skip();
});
it('test for ' + x, function (done) {
if (x === 'b') {
go_on=false;
console.log('stopping tests');
}
done();
});
});
});
The resulting output is: 结果输出为:
$ mocha test.js
conditional tests
✓ test for a
stopping tests
✓ test for b
- test for c
2 passing (13ms)
1 pending
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.