Karma vs testing framework Jasmine, Mocha, QUnit

Lee Chee Kiam picture Lee Chee Kiam · Sep 25, 2014 · Viewed 97.1k times · Source

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 reference shows is for unit test, however this said is for e2e test.

Answer

phtrivier picture phtrivier · Sep 25, 2014

Karma is a browser test runner.

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...

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.

(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.

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

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

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

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)