javascript javascript中的断言库、测试框架和测试环境有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25678063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between assertion library, testing framework and testing environment in javascript?
提问by Nader
采纳答案by Zach Mertes
Assertion libraries are tools to verify that things are correct.
This makes it a lot easier to test your code, so you don't have to do thousands of if
statements.
Example (using should.js and Node.js assert module):
断言库是验证事情是否正确的工具。
这使得测试您的代码变得更加容易,因此您不必执行数千条if
语句。
示例(使用 should.js 和 Node.js assert 模块):
var output = mycode.doSomething();
output.should.equal('bacon'); //should.js
assert.eq(output, 'bacon'); //node.js assert
// The alternative being:
var output = mycode.doSomething();
if (output !== 'bacon') {
throw new Error('expected output to be "bacon", got '+output);
}
Testing frameworks are used to organize and execute tests.
Mocha and Jasmine are two popular choices (and they're actually kinda similar).
Example (using mocha with should.js here):
测试框架用于组织和执行测试。
Mocha 和 Jasmine 是两种流行的选择(实际上它们有点相似)。
示例(此处使用 mocha 和 should.js):
describe('mycode.doSomething', function() {
it ('should work', function() {
var output = mycode.doSomething();
output.should.equal('bacon');
});
it ('should fail on an input', function() {
var output = mycode.doSomething('a input');
output.should.be.an.Error;
});
});
Testing Environments are the places where you run your tests.
测试环境是您运行测试的地方。
Karma is a bit of an edge case, in the sense that it's kind of a one off tool, not many like it. Karma works by running your unit tests inside of browsers (defaulting to PhantomJS, a headless WebKit browser), to allow you to test browser-based JavaScript code.
Karma 有点极端,因为它是一种一次性工具,喜欢它的人并不多。Karma 通过在浏览器(默认为 PhantomJS,一种无头 WebKit 浏览器)内运行您的单元测试来工作,以允许您测试基于浏览器的 JavaScript 代码。
Frameworks like Mocha and Jasmine work both in the browser and with Node.js, and usually default to Node.
像 Mocha 和 Jasmine 这样的框架既可以在浏览器中使用,也可以与 Node.js 一起使用,并且通常默认使用 Node。
回答by Jeff Storey
The testing environment (or test runner) is what runs all of your tests. It launches them, aggregates results, etc.
测试环境(或测试运行器)运行所有测试。它启动它们,聚合结果等。
The testing framework is what you use to create each of the tests. For example, jasmine uses a syntax of
测试框架是您用来创建每个测试的框架。例如,茉莉花使用的语法为
it('name of test', function() {
// do some tests
});
The assertion library is what does the actual verification of your test results
断言库是对你的测试结果进行实际验证的
it('name of test', function() {
assert x == 5 //pseudocode, the syntax will vary based on your asserting framework
});