我为构造函数编写了测试集。此构造函数创建并初始化一些属性。我的测试测试了每个属性。我还有一些数组,其中包含测试属性的名称。我的测试在该数组中添加了他们测试的属性名称。
在完成测试集之后必须启动我的特殊测试:它将我的构造函数定义的属性集与包含测试属性集的数组的内容进行比较。如果测试了所有属性,则测试通过,否则失败。
上次测试允许我确定我测试了所有属性。稍后如果我添加新属性,那么此测试会立即让我知道我要添加新测试或测试集。
(function(){
let instanceCheckedProps = [];
QUnit.test("Node() ctor: check the instance's id property.",
function (assert) {
let prop = 'id';
// here is my test code...
instanceCheckedProps.push(prop);
});
// Here is my other tests...
QUnit.test('Node() ctor : check if all properties have been tested',
function (assert) {
let node = new Tree.Node();
let allPropsChecked = true;
let instanceAllProps = Object.keys(node);
for (let i = 0; i < instanceAllProps.length; i++) {
if (!instanceCheckedProps.includes(instanceAllProps[i])) {
allPropsChecked = false;
break;
}
}
assert.ok(allPropsChecked);
});
})();
但我发现QUnit启动我的测试的次数与编写的次序不同:我的特殊测试首先启动。
我该如何解决?
答案 0 :(得分:0)
This is expected, and correct, behavior. In ANY testing scenario, the tests should be atomic. In other words, you should not make one test depend on others. Your last test is NOT a test at all, and should not be included. Instead, you should adopt a development strategy where you write the test BEFORE you write the code to ensure that there is always a test for each property.
What you have above is an anti-pattern and will lead to more problems!! I highly encourage you to not do this.
That all said, if you are unwilling to change your practices and feel that you just want to implement the workaround and not do things correctly, then you can explore the reorder
configuration option in QUnit.