Javascript Node.js 单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7254025/
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
Node.js Unit Testing
提问by Mark
Are there any good node.js (server side js) unit testing frameworks currently out there? I'm looking for something a little deeper than the Assert module provided.
目前是否有任何好的 node.js(服务器端 js)单元测试框架?我正在寻找比 Assert 模块提供的更深入的东西。
回答by evilcelery
I ended up using node-unitand am really happy with it.
我最终使用了node-unit并且对它非常满意。
I was using Expressooriginally but the fact that it runs tests in parallel caused a few problems. (For example using database fixtures doesn't work well in this situation).
我最初使用的是Expresso,但它并行运行测试的事实导致了一些问题。(例如,在这种情况下,使用数据库装置不能很好地工作)。
回答by bennidi
I was also looking for a decent test framework for node and found Mocha. It is the official successor to Expressoand seems very mature.
我也在寻找一个像样的 node 测试框架并找到了 Mocha。它是Expresso的官方继承者,看起来非常成熟。
It allows to plug-in different assertion libraries, it offers reporters for code coverage and other things (you can plug-in your own). It can run sync or async and it has a concise API.
它允许插入不同的断言库,它为代码覆盖和其他事情提供报告器(您可以插入自己的)。它可以运行同步或异步,并且有一个简洁的 API。
I will give it a try and report back...
我会试一试,然后回来报告……
EDIT:
编辑:
After an incredible amount of time dedicated to other projects I finally came back to a Javascript project and had time to play around with mocha. I can seriously recommend using it. The tests read very nicely, integration with gulp is great and tests run very fast. I was able to setup automatic standalone as well as in-browser (browserify) test runs and corresponding code coverage reports in about half a day (most of the time spent on understanding how to use browserify from gulp). To me, mocha seems a very good choice for a testing framework.
在致力于其他项目的大量时间之后,我终于回到了 Javascript 项目并有时间玩弄 mocha。我可以认真推荐使用它。测试读起来非常好,与 gulp 的集成很棒,测试运行速度非常快。我能够在大约半天的时间内设置自动独立以及浏览器内(browserify)测试运行和相应的代码覆盖率报告(大部分时间花在了解如何从 gulp 中使用 browserify)。对我来说,mocha 似乎是一个非常好的测试框架选择。
UPDATE:
更新:
I am still very convinced about Mocha. Integration with chai allows to plugin different assertion styles. You can checkout a working setup in this github project. I am using it with karma now, integrating code coverage report, automatic watchers and good integration with IntelliJ
我对摩卡还是很有信心的。与 chai 的集成允许插入不同的断言样式。你可以在这个 github 项目中签出一个工作设置。我现在将它与 karma 一起使用,集成了代码覆盖率报告、自动观察器以及与 IntelliJ 的良好集成
回答by einaros
Personally I've stuck with Expresso, but there are a bunch of different frameworks out there, accommodating most testing styles.
就我个人而言,我一直坚持使用Expresso,但是那里有很多不同的框架,可以适应大多数测试风格。
Joyent has an extensive list; give that a go.
Joyent 有一个广泛的列表;试一试。
回答by jtsao22
I've personally only used the assert module, but also find myself wanting more. I've looked through many node modules and popular unit testing frameworks are node-unitand should (which is made by the same guy as Expresso (maybe an updated name?)
我个人只使用了 assert 模块,但也发现自己想要更多。我查看了许多节点模块,流行的单元测试框架是节点单元和应该(由与 Expresso 相同的人制作(也许是更新的名称?)
Vowsalso looks promising.
誓言看起来也很有希望。
回答by Raynos
vowsis a solid unit testing library for node.js but the syntax is tedious.
誓言是一个可靠的 node.js 单元测试库,但语法很乏味。
I've written a thin abstraction called vows-fluentwhich makes the API chainable.
我已经编写了一个称为vues-fluent的薄抽象,它使 API 可链接。
And I've written another abstraction, [vows-is] which builds on vows-fluent and exposes a BDD style syntax.
我已经写了另一个抽象,[vows-is],它建立在 vows-fluent 基础上,并公开了 BDD 风格的语法。
An example would be
一个例子是
var is = require("vows-is");
is.suite("testing is fun").batch()
.context("is testing fun?")
.topic.is("yes")
.vow.it.should.equal("yes")
.suite().run({
reporter: is.reporter
});
回答by sam100rav
I think among various testing frameworks available, mochais the most latest, and very simple to implement. Here is a wonderful tutorial about how to use it: http://thewayofcode.wordpress.com/2013/04/21/how-to-build-and-test-rest-api-with-nodejs-express-mocha/
我认为在各种可用的测试框架中,mocha是最新的,并且非常易于实现。这是一个关于如何使用它的精彩教程:http: //thewayofcode.wordpress.com/2013/04/21/how-to-build-and-test-rest-api-with-nodejs-express-mocha/
回答by Zorayr
If you are familiar with QUnit, you could use node-qunitwhich is a sort of a node wrapper around QUnit's existing framework.
如果您熟悉QUnit,则可以使用node-qunit,它是一种围绕 QUnit 现有框架的节点包装器。
回答by B T
Originally made for node.js, deadunitis a javascript unit testing library for node.js and the browser. Some of its unique attributes:
deadunit最初是为 node.js 开发的,是一个用于 node.js 和浏览器的 javascript 单元测试库。它的一些独特属性:
- Easy learning curve
- Can output test results on the command line (colored or plain-text) or as html
- It prints out the actual lines of code where your assertions are, so your output makes sense even if you don't spend a lot of time writing test commentary
- It has a simple
count
assertion that makes dealing with expected exceptions and asynchronous asserts easy - it prints out exception andany attached data they have
- it'll let you know if your code is hanging (something you don't want, but usually goes unnoticed)
- Has an event driven API enables streaming test results across a network, or in any way you want.
- Supports testing with node-fibers
- 简单的学习曲线
- 可以在命令行(彩色或纯文本)或 html 上输出测试结果
- 它打印出断言所在的实际代码行,因此即使您不花大量时间编写测试评论,您的输出也有意义
- 它有一个简单的
count
断言,可以轻松处理预期的异常和异步断言 - 它打印出异常和他们拥有的任何附加数据
- 它会让你知道你的代码是否挂起(你不想要的东西,但通常会被忽视)
- 拥有事件驱动的 API,可以通过网络或以您想要的任何方式流式传输测试结果。
- 支持使用节点光纤进行测试
回答by Dale Anderson
test-studiois an npm package that provides a powerful, web based front end for unit testing. It supports things like executing individual or groups of tests and stepping node-inspector into individual tests. It currently supports mocha and more frameworks will be supported in future given demand.
test-studio是一个 npm 包,它为单元测试提供了一个强大的、基于 Web 的前端。它支持执行单个或一组测试以及将节点检查器步进到单个测试中。它目前支持 mocha,未来根据需求将支持更多框架。
Read more about it here.
在此处阅读更多相关信息。
Disclaimer: I am the author.
免责声明:我是作者。
回答by noamtcohen
I just uploaded a project I am using to unit test nodejs with karma and Jasmine: narma. Your node modules get loaded into a nodewebkit browser so you can execute node modules and use libraries like jquery in the same heap.
我刚刚上传了一个项目,我正在使用 karma 和 Jasmine 对nodejs进行单元测试:narma。您的节点模块被加载到 nodewebkit 浏览器中,因此您可以执行节点模块并在同一个堆中使用像 jquery 这样的库。