与 Node.js 一起使用的最佳测试框架是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4308786/
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 is the best testing framework to use with Node.js?
提问by doffm
I have looked at the rather long list of testing frameworks at https://github.com/ry/node/wiki/modules#testing. What is the experience with these frameworks?
我在https://github.com/ry/node/wiki/modules#testing查看了相当长的测试框架列表。使用这些框架有何经验?
Obviously the ability to run in the browser would be a big bonus, but I'm mainly interested in Node.js. Something with a heavily asynchronous slant would be great.
显然,在浏览器中运行的能力将是一个很大的好处,但我主要对 Node.js 感兴趣。具有严重异步倾斜的东西会很棒。
回答by Alfred
Update:
更新:
Mochais the best in my opinion.
摩卡在我看来是最好的。
What is the experience with these frameworks?
使用这些框架有何经验?
I played with expressowhich is pretty cool testing framework which also has test-coverage. It has been created by TJ Holowaychuk who is also the creator of Express.js(insanely fast (and small) server-side JavaScript web development framework built on Node.js and Connect). I recently saw that he also has a cool library called should.jswhich can be used together with Expresso for a even better testing experience.
我玩过expresso,它是一个非常酷的测试框架,它也有测试覆盖。它由 TJ Holowaychuk 创建,他也是Express.js(基于 Node.js 和 Connect 构建的快速(且小巧)的服务器端 JavaScript Web 开发框架)的创建者。我最近看到他还有一个很酷的库,叫做should.js,可以和 Expresso 一起使用以获得更好的测试体验。
Obviously, the ability to run in the browser would be a big bonus
显然,在浏览器中运行的能力将是一个很大的好处
I don't believe it can run in the browser, but I also don't get why you would want to run it inside the browser?
我不相信它可以在浏览器中运行,但我也不明白为什么要在浏览器中运行它?
but I'm mainly interested in Node.js. Something with a heavily asynchronous slant would be great.
但我主要对 Node.js 感兴趣。具有严重异步倾斜的东西会很棒。
Quote from the expresso:
来自快递的报价:
The argument passed to each callback is beforeExit, which is typically used to assert that callbacks have been invoked.
传递给每个回调的参数是 beforeExit,它通常用于断言回调已被调用。
You can use beforeExit to test asynchronous functions.
您可以使用 beforeExit 来测试异步函数。
TIP:Follow TJ Holowaychukon GitHub, because he creates very good open-source code.
提示:在GitHub 上关注TJ Holowaychuk,因为他创建了非常好的开源代码。
回答by panosru
I use VowsJSwhich is easy to use async BDD framework (Behaviour Driven Development) and get the job done.
我使用VowsJS,它易于使用异步 BDD 框架(行为驱动开发)并完成工作。
From what I see lately it's what many chose to test their NPM modules, so I believe so far it's one the best to use.
从我最近看到的情况来看,许多人选择测试他们的 NPM 模块,所以我相信到目前为止它是最好的之一。
Some popular testing frameworks that could be used with NodeJS are also those:
一些可以与 NodeJS 一起使用的流行测试框架也是那些:
You can also see a list of JavaScript test frameworks here
您还可以在此处查看 JavaScript 测试框架列表
Few more libs that could help you write better code are those:
可以帮助您编写更好代码的更多库是:
- ReadyJSwatches your js files and test them with JSHint
- Concretesmall continuous integrationserver
- Jezebelcontinuous testing for Jasmine
- Noseynot quite there but have a nice roadmap so I keep an eye on it
There is also Bamboo CI Serverby Atlassianit automates builds and tests. It is a package for Apache/Tomcat(which sux because it uses Java and that makes it very heavy)also is not free but it has a starter license which costs $10so I believe it is affordable. It is the most featured of all CI servers I found so far and it supports all unit tests that support xUnitthat means that you can run builds/tests for any language with Bamboo.
还有Atlassian 的Bamboo CI Server,它可以自动构建和测试。它是一个用于Apache/Tomcat的包(它很糟糕,因为它使用 Java 并且使它非常重)也不是免费的,但它有一个售价 10 美元的入门许可证,所以我相信它是负担得起的。它是迄今为止我发现的所有 CI 服务器中功能最强大的,它支持所有支持xUnit 的单元测试,这意味着您可以使用 Bamboo 为任何语言运行构建/测试。
Another option for CI with NodeJS is Traviswhich lot of people use for their open source projects, as it says A hosted continuous integration service for the open source community.
使用 NodeJS 进行 CI 的另一个选择是Travis,很多人将其用于他们的开源项目,正如它所说的为开源社区提供托管的持续集成服务。
There is also a google group discussion with Continuous Integration for Node JS Projectstopic.
还有一个关于 Node JS 项目持续集成主题的 google 小组讨论。
回答by Chris Jaynes
Based on the asker's comments above, I tried out vows, and it solved a lot of problems I was having with my async testing. Its ability to mix serial and parallel testing is awesome.
根据上面提问者的评论,我尝试了誓言,它解决了我在异步测试中遇到的很多问题。它混合串行和并行测试的能力非常棒。
Make sure you read the guidance doc carefully, but once you get the hang of it, it's flexible, powerful, and produces nice, clean results.
请务必仔细阅读指导文档,但一旦掌握了它的窍门,它就会灵活、强大,并且会产生漂亮、干净的结果。
UPDATE: I would also encourage people to check out shouldfor their asserts. It allows for very flexible, very readable asserts, and is compatible with both Expresso and Vows, and probably most other test frameworks as well.
更新:我也鼓励人们检查他们的断言应该。它允许非常灵活、非常易读的断言,并且与 Expresso 和 Vows 兼容,也可能与大多数其他测试框架兼容。
(I'm posting this as a separate answer just in case people don't notice the comments on Alfred's answer.)
(我将此作为单独的答案发布,以防人们没有注意到对 Alfred 答案的评论。)
UPDATE 1/7/2015: For what it's worth, I have since switched from Vows to Mocha, and from Should to Chai. Mocha has much better support now for asynchronous tests using promises, and Chai allows for several flexible assert options, including the expectapi, for those who don't like to modify the object prototype.
2015 年 1 月 7 日更新:就其价值而言,此后我已从 Vows 切换到 Mocha,从 Should 切换到 Chai。Mocha 现在对使用 promise 的异步测试有了更好的支持,并且 Chai 允许expect为那些不喜欢修改对象原型的人提供多种灵活的断言选项,包括api。
回答by Mark B
I've started using Jasminefor my JavaScript testing specifically because it's small and runs in both the browser and node. It's also got a really solid reporting and matcher API so it's easy to integrate with other tools in the future. Having a buildin mocking framework is also useful since it's often one of the first things I would add when I was using qunit for TDD in the browser.
我已经开始使用Jasmine进行 JavaScript 测试,特别是因为它很小并且可以在浏览器和节点中运行。它还具有非常可靠的报告和匹配器 API,因此将来很容易与其他工具集成。内置模拟框架也很有用,因为它通常是我在浏览器中使用 qunit 进行 TDD 时首先要添加的内容之一。
回答by Vanwaril
I've been using nodeunitand its ability to work with async functions is reasonably straightforward.
我一直在使用nodeunit,它使用异步函数的能力相当简单。
There's a nice walkthrough that should get you ready-to-go with nodeunit on his blog.
在他的博客上有一个很好的演练,可以让您准备好使用 nodeunit 。
[ Note:the API has changed since the blogpost – setUp(callback)and tearDown(callback)both take a callback as an argument, which you need to call when your setup/teardown is complete. ]
[注:由于博客帖子的API已经改变-setUp(callback)而且tearDown(callback)都采取了回调作为参数,你需要在你的安装/拆卸完成后调用。]
回答by user2434418
If you want a true BDD framework then maybe consider Yadda. It integrates with mocha, jasmine, nodeunit, qunit, zombie and casperjs, to support feature files, e.g.
如果你想要一个真正的 BDD 框架,那么也许可以考虑Yadda。它集成了 mocha、jasmine、nodeunit、qunit、zombie 和 casperjs,以支持特征文件,例如
Scenario: provides the version of all services
given service x is running
and service y is running
when I request the service versions
then service x should be version 0.0.1
and service y should be version 0.0.2

