Introduction to QUnit
QUnit is a powerful, easy-to-use JavaScript testing framework primarily used for unit testing JavaScript code. It provides a simple yet effective API to write tests for your applications, making it one of the most preferred choices among developers.
Setting Up QUnit
To get started, you need to include the QUnit library in your project. You can download it or link to it via a CDN:
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.18.0.css"> <script src="https://code.jquery.com/qunit/qunit-2.18.0.js"></script>
Basic Example
Here is a simple example to get you started with QUnit.
<div id="qunit"></div> <div id="qunit-fixture"></div> <script> QUnit.test("hello test", function(assert) { assert.ok(1 == "1", "Passed!"); }); </script>
Dozens of Useful APIs
QUnit offers a variety of APIs to enhance your testing experience:
1. QUnit.module
Use this to group related tests.
QUnit.module("group A"); QUnit.test("test A1", function(assert) { assert.ok(true, "Passed A1!"); }); QUnit.test("test A2", function(assert) { assert.ok(true, "Passed A2!"); }); QUnit.module("group B"); QUnit.test("test B1", function(assert) { assert.ok(true, "Passed B1!"); });
2. QUnit.test
The most basic way to define a test.
QUnit.test("example test", function(assert) { assert.equal(2 + 2, 4, "Two plus two equals four."); });
3. QUnit.skip
Skip a test that you don’t want to run as part of the current suite.
QUnit.skip("skipped test", function(assert) { assert.ok(false, "This test will not run."); });
4. QUnit.todo
Mark a test as expected to fail.
QUnit.todo("todo test", function(assert) { assert.ok(false, "This test is expected to fail."); });
5. assert.expect
Specify how many assertions are expected in a test.
QUnit.test("test with expect", function(assert) { assert.expect(2); assert.ok(true, "First assertion!"); assert.ok(true, "Second assertion!"); });
Simple Application Example
Let’s consider a simple calculator application and write tests for its functionalities.
// Calculator code function Calculator() { this.add = function(a, b) { return a + b; }; this.subtract = function(a, b) { return a - b; }; this.multiply = function(a, b) { return a * b; }; this.divide = function(a, b) { return a / b; }; }
// QUnit tests for Calculator QUnit.module("Calculator Tests"); QUnit.test("Addition Test", function(assert) { let calc = new Calculator(); assert.equal(calc.add(1, 1), 2, "1 + 1 equals 2"); }); QUnit.test("Subtraction Test", function(assert) { let calc = new Calculator(); assert.equal(calc.subtract(2, 1), 1, "2 - 1 equals 1"); }); QUnit.test("Multiplication Test", function(assert) { let calc = new Calculator(); assert.equal(calc.multiply(2, 3), 6, "2 * 3 equals 6"); }); QUnit.test("Division Test", function(assert) { let calc = new Calculator(); assert.equal(calc.divide(4, 2), 2, "4 / 2 equals 2"); });
With these examples, you can see how QUnit simplifies JavaScript testing and ensures your code works as expected. Happy testing!
Hash: 539755620ac7e6cb1a31afe39bd1178c9f2eddc3286a4d817bd3f51d43a58686