如何在所有浏览器中运行单元测试?
我从未使用过 Selenium,但我猜它是用于模拟所有浏览器中的用户交互。
这就像集成测试。
但是如何以自动化的方式在所有浏览器上测试您的 js 库/框架(单元测试)?
I've never used Selenium but I guess it's for simulating user interaction in all browsers.
That's like integration tests.
But how do you test your js libraries/frameworks (unit testing) on all the browsers in an automated way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
对于单元测试,您可以尝试 http://code.google.com/p/ js-测试驱动程序/
For unit-testing you can try http://code.google.com/p/js-test-driver/
您可以查看 TestSwarm:
You can have a look at TestSwarm:
我认为最好的一个是来自 YUI 的: http://developer.yahoo.com/yui/3/test/< /a>
但是在每个浏览器中进行单元测试有点困难......大多数人只是在开发过程中使用它进行测试,然后使用 Node.js 进行测试,以防万一他们破坏了某些东西。
The best one imo is the one from YUI : http://developer.yahoo.com/yui/3/test/
But doing unit-testing in every browser is kind of hard... Most people just test with it during development and just use node.js to test later on in case they broke something.
正如上一篇文章中引用的帖子所建议的,您可以使用 js-test-driver。
它专门用于跨多个浏览器的 JavaScript 单元测试,正是您想要的。我已经弄乱了它并且它非常好。不过我还没有做过任何认真的商业测试。
As the referenced post in a previous article suggests, you could use js-test-driver.
Its specifically for JavaScript unit testing across multiple browsers, exactly what you want. I have messed around with it and it is pretty good. I haven't done any serious commercial testing in it though.
对于单元测试,您应该考虑不加载浏览器来进行测试的解决方案。
您可以查看 RhinoUnit 之类的东西 - http://code.google.com/p/rhinounit< /a>
另请参阅 Dojo Object Harness (DOH) 单元测试框架 http://dojotoolkit.org/reference-guide/util/doh.html
在这里查看一个类似的问题,它可以让您了解如何使用 TDD js - 用于 TDD 的 JavaScript 单元测试工具
For unit testing you should look at solutions that do not load up a browser to do the tests.
You can look at something like RhinoUnit for that - http://code.google.com/p/rhinounit
Also have a look at Dojo Object Harness (DOH) unit test framework http://dojotoolkit.org/reference-guide/util/doh.html
Look at a similar question here which can give you an idea on how to TDD js - JavaScript unit test tools for TDD
它不再被积极维护,但我仍然对 JSUnit 进行 Javascript 单元测试感到满意: https://github.com /pivotal/jsunit
它包括一个可以在浏览器中运行的 HTML/Javascript 框架,以及一个可以从 ant 调用的基于 java 的测试运行程序。
It's no longer actively maintained, but I've still been happy with JSUnit for Javascript unit testing: https://github.com/pivotal/jsunit
It includes both an HTML/Javascript framework you can run in the browser, and a java-based test runner that you can invoke from ant.
要并行测试多个浏览器,您可以使用 Selenium Grid。请看这里:http://selenium-grid.seleniumhq.org/step_by_step_installation_instructions_for_windows.html 有关如何使用它的分步说明。
To test multiple browsers in parallel you would Selenium Grid. Please take a look here: http://selenium-grid.seleniumhq.org/step_by_step_installation_instructions_for_windows.html for step by step instructions on how to use it.
到目前为止我还没有使用过它,它仍然处于测试阶段,但是 FuncUnit 将自己声明为“基于 qUnit、Selenium 和 jQuery 的功能测试套件”。
还有一个信息图解释它是如何工作的
也许这就是你想要的东西? github 存储库 似乎相当活跃。
I haven't used it so far, and it is still in beta, but FuncUnit declares itself as "A functional test suite based of qUnit, Selenium and jQuery".
There is also an infographic explaining how it works
Maybe it is something what you want? The github repo seems to be quite active.
您应该检查 http://saucelabs.com 它是一个云基础 selenium 环境,允许您构建测试然后上传它们,在任意数量的浏览器中运行它们。
You should check http://saucelabs.com its a cloud base selenium environment that allow you to build your test and then upload them, run them in as many browsers as you want.
您应该考虑 crossbrowsertesting.com 提供的功能
(它不是免费的)。对于 js 测试,请尝试 JsUnit (http://www.jsunit.net/)。
引自其主页
JsUnit 是客户端(浏览器内)JavaScript 的单元测试框架。它本质上是 JUnit 到 JavaScript 的端口。还包括一个用于在多个浏览器和运行不同操作系统的多台计算机上自动执行测试的平台。
You should consider the capabilities offered by crossbrowsertesting.com
(It is not for free). For js testing try JsUnit (http://www.jsunit.net/).
Quoted from its homepage
JsUnit is a Unit Testing framework for client-side (in-browser) JavaScript. It is essentially a port of JUnit to JavaScript. Also included is a platform for automating the execution of tests on multiple browsers and mutiple machines running different OSs.
正如人们已经说过的,你应该使用 JsTestDriver。它有自己的测试系统,但您可以使用其他测试库,例如 Jasmine ( http://pivotal .github.com/jasmine/ )。您可以在此处找到 JsTestDriver 适配器列表:http://code。 google.com/p/js-test-driver/wiki/XUnitCompatibility
As people have already said so, you should use JsTestDriver. It has it's own test system, but you can use other test libraries with it, for example Jasmine ( http://pivotal.github.com/jasmine/ ). You can find a list of adaptors for JsTestDriver here: http://code.google.com/p/js-test-driver/wiki/XUnitCompatibility
我会选择 QUnit,这是 jQuery 使用的。我已经在很多桌面浏览器以及 iPhone 和 Android 手机上运行过它。
有一些很棒的教程 它可以轻松地与 js-test-driver 之类的东西集成。 QUnit 是根据 JUnit 和所有其他 xUnit 测试框架(如 PHPUnit)建模的,因此很容易掌握 API。
基本语法如下:
它看起来也很不错:
I'd go for QUnit, which is what jQuery uses. I've ran it on lots of desktop browsers as well as iPhones and Android phones.
There's some great tutorials and it can be integrated easily with things like js-test-driver. QUnit is modeled after JUnit and all the other xUnit testing frameworks (like PHPUnit) so it's easy to pick up the API.
The basic syntax is as follows:
It also looks quite nice:
专门从事跨浏览器测试的公司很少。
http://browserling.com/
http://www.browserstack.com/
http://saucelabs.com/ (此处已提到)
http://browsershots.org/
http://www.browsercam.com/
http://browserseal.com /
使用您想要的任何测试库。
There are few companies that specialize in cross browser testing.
http://browserling.com/
http://www.browserstack.com/
http://saucelabs.com/ (already mentioned here)
http://browsershots.org/
http://www.browsercam.com/
http://browserseal.com/
Use whatever testing lib you want.
Selenium 或 SauceLabs 等不是单元测试。它们是功能/集成测试解决方案。
您需要抽象 DOM 等外部用法来对 javascript 进行单元测试。
编写您的测试,以便它们可以通过配置使用任何外部库,例如 jquery。这样,您就可以对逻辑进行单元测试,而无需接触任何外部性,并且还可以测试跨浏览器测试。
Selenium or SauceLabs etc are not unit testing. They are functional/integration testing solutions.
You need to abstract your external usages like DOM to unit test javascript.
Write your tests so that they can use any external library like jquery by configuration. So that, you can unit test your logic without touching any externality and you can also both test cross-browser testing.