QUnit:每个方法进行一次测试,有多个断言,还是每个方法进行多个测试?
我决定为我的下一个 javascript 项目开始 TDD,并且我使用 QUnit 进行单元测试。我对单元测试完全陌生,从未用任何语言做过。下面是我的一个模块的示例,加上一个针对 find
方法的测试,该方法尝试涵盖此方法将遇到的所有场景:
module("TextSwapper", {
setup: function() {
this.str = 'When you first start off trying to solve a problem, the first solutions you come up with are very complex, and most people stop there. But if you keep going, and live with the problem and peel more layers of the onion off, you can often times arrive at some very elegant and simple solutions.';
this.ts = new TextSwapper();
ok(this.ts, 'The TextSwapper was created successfully');
this.textarea = document.createElement('textarea');
this.textarea.value = this.str;
document.body.appendChild(this.textarea);
},
teardown: function() {
document.body.removeChild(this.textarea);
}
});
test("find()", function() {
equal(this.ts.find('When'), false, "Check it fails gracefully when no input has been set.");
this.textarea.focus();
equal(this.ts.find('When'), true, "Check it finds 'When' if the textarea has focus but no input has been set.");
this.ts.setInput(this.textarea);
this.ts.find('When');
equal(window.getSelection().toString(), 'When', 'The word \'When\' should be highlighted');
equal(this.ts.found[0][0], 'When', 'The word \'When\' should be in the found array');
this.ts.find('you');
equal(window.getSelection().toString(), 'you', 'The word \'you\' should be highlighted');
equal(this.ts.found[1][0], 'you', 'The word \'you\' should be in the found array');
equal(this.ts.found.length, 4 ,'Should have found 4 you\'s');
this.ts.find('bill');
equal(this.ts.found.length, 0, 'Check that nothing was found for \'bill\'');
this.ts.find('[a-z]*ee[a-z]*');
equal(window.getSelection().toString(), 'keep', 'The word \'keep\' should be highlighted');
equal(this.ts.found[1][0], 'peel', 'The word \'peel\' should be in the found array');
equal(this.ts.found.length, 2 ,'Should have found 2 for [a-z]*ee[a-z]*');
});
我的问题是我是否以正确的方式处理此问题?我的测试中是否有太多断言?我的测试是否应该分解为更小的测试?我一直在 stackoverflow 上阅读 TDD,现在读到的一些东西让我觉得我做错了。
I've decided to embark on TDD for my next javascript project and I'm using QUnit for the unit testing. I am completely new to unit testing, having never done it in any language. Here is an example of one of my modules plus one test for the find
method that tries to cover all scenarios this method will encounter:
module("TextSwapper", {
setup: function() {
this.str = 'When you first start off trying to solve a problem, the first solutions you come up with are very complex, and most people stop there. But if you keep going, and live with the problem and peel more layers of the onion off, you can often times arrive at some very elegant and simple solutions.';
this.ts = new TextSwapper();
ok(this.ts, 'The TextSwapper was created successfully');
this.textarea = document.createElement('textarea');
this.textarea.value = this.str;
document.body.appendChild(this.textarea);
},
teardown: function() {
document.body.removeChild(this.textarea);
}
});
test("find()", function() {
equal(this.ts.find('When'), false, "Check it fails gracefully when no input has been set.");
this.textarea.focus();
equal(this.ts.find('When'), true, "Check it finds 'When' if the textarea has focus but no input has been set.");
this.ts.setInput(this.textarea);
this.ts.find('When');
equal(window.getSelection().toString(), 'When', 'The word \'When\' should be highlighted');
equal(this.ts.found[0][0], 'When', 'The word \'When\' should be in the found array');
this.ts.find('you');
equal(window.getSelection().toString(), 'you', 'The word \'you\' should be highlighted');
equal(this.ts.found[1][0], 'you', 'The word \'you\' should be in the found array');
equal(this.ts.found.length, 4 ,'Should have found 4 you\'s');
this.ts.find('bill');
equal(this.ts.found.length, 0, 'Check that nothing was found for \'bill\'');
this.ts.find('[a-z]*ee[a-z]*');
equal(window.getSelection().toString(), 'keep', 'The word \'keep\' should be highlighted');
equal(this.ts.found[1][0], 'peel', 'The word \'peel\' should be in the found array');
equal(this.ts.found.length, 2 ,'Should have found 2 for [a-z]*ee[a-z]*');
});
My question is am I going about this the right way? Do I have too many assertions in my test? Should my tests be broken down into even smaller tests? I've been reading up on TDD on stackoverflow and I have read a few things now that make me feel like I'm doing this wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 TDD,那么您应该为每种测试方法使用一个断言。
以下链接很好地解释了用一种方法测试所有内容时可能遇到的问题:单元测试 它可以更轻松地在代码中引入隐藏的错误。
If you are using TDD then you should go for one assertion for each test method.
The following link has a nice explanation of the problems you can run into when testing everything in one method: Unit testing It could more easily introduce hidden bugs in your code.
如果你能找到 Bob Martin 的《Clean Code》的副本,他在单元测试的章节中讨论了这个问题。他的观点是“每个测试一个断言”可能有点尴尬(他在书中给出了一个很好的例子),而他更喜欢“每个测试一个概念”。因此,如果您觉得多个断言密切相关并且分开会很烦人,请继续使用它们。
If you can find a copy of Bob Martin's "Clean Code", he discusses this question in the chapter on unit testing. His opinion is that "one assertion per test" can be a bit awkward (he gives a good example in the book), and he prefers to shoot for "one concept per test" instead. So go ahead and use multiple assertions if you feel that they are closely related and would be annoying to separate.