Javascript Karma 与测试框架 Jasmine、Mocha、QUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26032124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:13:25  来源:igfitidea点击:

Karma vs testing framework Jasmine, Mocha, QUnit

javascriptunit-testingintegration-testing

提问by Lee Chee Kiam

Few questions:

几个问题:

  • How Karma and testing framework X (Jasmine, Mocha, QUnit) relate to each other?
  • What is the equivalent framework at Java world? I assume Jasmine, Mocha, QUnit equal to jUnit/TestNG. How about Karma?
  • Can I run testing framework X (e.g. Jasmine) without Karma?
  • Is Karma for unit test or integration/e2e test? This referenceshows is for unit test, however thissaid is for e2e test.
  • Karma 和测试框架 X(Jasmine、Mocha、QUnit)如何相互关联?
  • Java 世界中的等效框架是什么?我假设 Jasmine、Mocha、QUnit 等于 jUnit/TestNG。卡玛呢?
  • 我可以在没有 Karma 的情况下运行测试框架 X(例如 Jasmine)吗?
  • Karma 是用于单元测试还是集成/e2e 测试?此参考显示用于单元测试,但是表示用于 e2e 测试。

回答by phtrivier

Karma is a browser test runner.

Karma 是一个浏览器测试运行器。

The idea is that browsers do not have natively a concept of loading tests files, running them, and reporting results. What karma does is (roughly) :

这个想法是浏览器本身没有加载测试文件、运行它们和报告结果的概念。业力的作用是(大致):

  • starting a small web server to serve "client-side" javascript files to be tested (1)
  • also serve the "client-side" javascript files with the tests (or Specs, as they're often called) (2)
  • serve a custom web page that will run javascript code for the tests (3)
  • start a browser to load this page (4)
  • report the results of the test to the server (5)
  • karma can then again report the results to text files, the console, anything your CI server likes, etc...
  • 启动一个小型 Web 服务器来为要测试的“客户端”javascript 文件提供服务 (1)
  • 还为“客户端”javascript 文件提供测试(或规范,因为它们通常被称为)(2)
  • 提供一个自定义网页,该网页将为测试运行 javascript 代码 (3)
  • 启动浏览器以加载此页面 (4)
  • 将测试结果报告给服务器 (5)
  • 然后 karma 可以再次将结果报告给文本文件、控制台、任何你的 CI 服务器喜欢的东西等等......

Looking at each part :

查看每个部分:

(1) Those files will be your actual js files ; you will tell karma how to load them. If you use requirejs, there is a karma plugin, and some config is needed.

(1) 这些文件将是您实际的 js 文件;你会告诉业力如何加载它们。如果你使用 requirejs,有一个 karma 插件,并且需要一些配置。

(2) Those tests can be written in a variety of Javascript testing framework (Jasmine, QUnit, Mocha) ; this is JS code that is run in the browser.

(2) 那些测试可以用多种Javascript测试框架(Jasmine、QUnit、Mocha)编写;这是在浏览器中运行的 JS 代码。

(3) The custom web page will be a bit different for each testing framework ; this is why karma has plugins for different frameworks.

(3) 每个测试框架的自定义网页会有所不同;这就是 karma 为不同框架提供插件的原因。

(4) Karma can launch the page in many browsers (FF, Chrome, or headless browsers like PhantomJs.)

(4) Karma 可以在许多浏览器(FF、Chrome 或 PhantomJs 等无头浏览器)中启动页面。

(5) Reporting to karma is, again, framework-dependant, and dealt with karma plugins.

(5) 向 karma 报告同样依赖于框架,并处理 karma 插件。

So to answer your questions :

所以回答你的问题:

  • in Java, most people use JUnit which is both a framework to write tests and run them, but does not have the problem of differentiating the environment in which tests are run and the one in which test reports are aggregated ; karma would be the missing piece between a JUnit Suite and a JUnit TestRunner
  • Yes, you can do everything that karma does "by hand" - pick one framework (jasmine, qunit, mocha) and follow instructions. The advantage of karma is that it provide a solution out-of-the-box, if you're in a standard setup.
  • Karma can be used for both unit test (with jasmine / qunit / whatever) and integration tests (which will use another API, like webdriver, to drive the browser)
  • 在Java中,大多数人使用JUnit,它既是一个框架来编写测试并运行它们,但不存在区分运行测试和聚合测试报告的环境的问题;karma 将是 JUnit Suite 和 JUnit TestRunner 之间的缺失部分
  • 是的,您可以“手动”完成 karma 所做的一切——选择一个框架(jasmine、qunit、mocha)并按照说明进行操作。karma 的优点是它提供了一个开箱即用的解决方案,如果您处于标准设置中。
  • Karma 可用于单元测试(使用 jasmine/qunit/whatever)和集成测试(将使用另一个 API,如 webdriver,来驱动浏览器)

回答by SystematicFrank

One shorter way to understand the difference:

理解差异的一种更简短的方法:

People testing with plain Jasmine / Mocha most likely are running all the code with the Node virtual machine.

使用普通 Jasmine / Mocha 进行测试的人很可能正在使用 Node 虚拟机运行所有代码。

Adding Karma to the mix (on top of your existing framework of choice) will run your test suite with the engine of other browsers.

将 Karma 添加到组合中(在您选择的现有框架之上)将使用其他浏览器的引擎运行您的测试套件。

By doing this you get the little extras you get with a browser environment. It will be easier to test DOM related code, but you will also be giving up to the extra resources given by the Node engine (like filesystem / shell access)

通过这样做,您可以获得浏览器环境中的一些额外功能。测试 DOM 相关代码会更容易,但您也会放弃 Node 引擎提供的额外资源(如文件系统/shell 访问)

回答by mauricio777

The thesis of the guy who designed Karma was very informative in describing existing solutions and comparing them, and of course describing Karma itself

设计 Karma 的人的论文在描述现有解决方案和比较它们方面非常有用,当然也描述了 Karma 本身

https://github.com/karma-runner/karma/blob/master/thesis.pdf

https://github.com/karma-runner/karma/blob/master/thesis.pdf

Summary: Karma is a test runner. It can be used by QUnit, Jasmine, Mocha, ... Karma has advantages to other test runners to improve your TDD/BDD development cycle. It "watches" files, so when you save a change, Karma runs tests and reports back instantly, no switching context to Web Browser to run the test.

总结: Karma 是一个测试运行器。它可以被 QUnit、Jasmine、Mocha 等使用…… Karma 比其他测试运行器更有优势,可以改善您的 TDD/BDD 开发周期。它“监视”文件,因此当您保存更改时,Karma 会运行测试并立即返回报告,无需将上下文切换到 Web 浏览器来运行测试。

In short, perhaps question should be Karma AND Jasmine or Mocha or QUnit?

简而言之,也许问题应该是 Karma AND Jasmine 或 Mocha 或 QUnit?