QUnit

Print Print
Reading time 3:57

QUnit
Initial release8 May 2008 (2008-05-08)
Stable release
2.15.0 / 12 April 2021; 2 months ago (2021-04-12)
Repository Edit this at Wikidata
Written inJavaScript
TypeTest automation framework
LicenseMIT
Websitequnitjs.com

QUnit is a JavaScript unit testing framework. Originally developed for testing jQuery, jQuery UI and jQuery Mobile, it is a generic framework for testing any JavaScript code. It supports client-side environments in web browsers, and server-side (e.g. Node.js).

QUnit's assertion methods follow the CommonJS unit testing specification, which itself was influenced to some degree by QUnit.

History

QUnit was originally developed by John Resig as part of jQuery. In 2008 it was extracted from the jQuery unit test source code to form its own project and became known as "QUnit". This allowed others to start using it for writing their unit tests. While the initial version of QUnit used jQuery for interaction with the DOM, a rewrite in 2009 made QUnit completely standalone.

Usage and examples

  • QUnit.module(string) - Defines a module, a grouping of one or more tests.
  • QUnit.test(string, function) - Defines a test.

QUnit uses a set of assertion method to provide semantic meaning in unit tests:[1]

  • assert.ok(boolean, string) - Asserts that the provided value casts to boolean true.
  • assert.equal(value1, value2, message) - Compares two values, using the double-equal operator.
  • assert.deepEqual(value1, value2, message) - Compares two values based on their content, not just their identity.
  • assert.strictEqual(value1, value2, message) - Strictly compares two values, using the triple-equal operator.

A basic example would be as follows:[2]

QUnit.test('a basic test example', function (assert) {
  var obj = {};

  assert.ok(true, 'Boolean true');       // passes
  assert.ok(1, 'Number one');            // passes
  assert.ok(false, 'Boolean false');     // fails

  obj.start = 'Hello';
  obj.end = 'Ciao';
  assert.equal(obj.start, 'Hello', 'Opening greet'); // passes
  assert.equal(obj.end, 'Goodbye', 'Closing greet'); // fails
});

See also

  • List of unit testing frameworks
  • Jasmine

References

  1. ^ "Assert methods". QUnit API Documentation. Retrieved 2018-02-14.
  2. ^ "Cookbook: Example test". QUnit API Documentation. Retrieved 2014-06-02.

External links

By: Wikipedia.org
Edited: 2021-06-18 12:37:06
Source: Wikipedia.org