Skip to content

Instantly share code, notes, and snippets.

@Potherca
Last active July 7, 2022 16:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Potherca/7946371 to your computer and use it in GitHub Desktop.
Save Potherca/7946371 to your computer and use it in GitHub Desktop.
A basic qUnit test example

This gist contains the minimum needed to run qUnit tests and a example of the most common features you will want to get aquanted with when you start writing tests.

It is viewable online at http://bl.ocks.org/potherca/7946371

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Tests</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="tests.js"></script>
</body>
</html>
/*global module, test, equal, expect, ok*/
/*
BASIC API DOCUMENTATION (for full API see http://docs.jquery.com/Qunit#API_documentation )
Setup:
test( name, [expected], test )
Add a test to run.
expect( amount )
Specify how many assertions are expected to run within a test.
module( name, [lifecycle] )
Separate tests into modules.
Assertions:
ok( state, [message] )
A boolean assertion, equivalent to JUnit's assertTrue. Passes if the first argument is truthy.
equal( actual, expected, [message] )
A comparison assertion, equivalent to JUnit's assertEquals.
notEqual( actual, expected, [message] )
A comparison assertion, equivalent to JUnit's assertNotEquals.
deepEqual( actual, expected, [message] )
A deep recursive comparison assertion, working on primitive types, arrays and objects.
notDeepEqual( actual, expected, [message] )
A deep recursive comparison assertion, working on primitive types, arrays and objects, with the result
inverted, passing when some property isn't equal.
strictEqual( actual, expected, [message] )
A stricter comparison assertion then equal.
notStrictEqual( actual, expected, [message] )
A stricter comparison assertion then notEqual.
raises( state, [message] )
Assertion to test if a callback throws an exception when run.
*/
// Basic Example
// These test are not included in a module
test("This is the name for this test", function () { // does not use the "expected" param
expect(1); // register how many assertions to Expect to be run within this test.
ok(true); // No message, so will just display 'okay'
});
// is the same as
test("This is another test", 1, function () { // use the "expected" param to register how many
// assertions to Expect to be run within this test.
ok(true, 'Look, a message!');
});
//Declare a module to group tests together in
module('My Module');
// The following tests will be part of a module called 'My Module'
test("1-2-3", function() {
expect(3);
var one = 1, two = 2, three = 3;
equal(one, 1, "1 equals 1");
equal(two, 2, "2 equals 2");
equal(three, 3, "3 equals 3");
});
test("4-5-6", function() {
expect(3);
var four = 4, five = 5, six = 6;
equal(four, 4, "4 equals 4");
equal(five, 5, "5 equals 5");
equal(six, 6, "6 equals 6");
});
module('All these tests will fail...');
test("This test will fail", 2, function () {
ok(false); // setting this to false will cause the test to fail
equal(1, 2, 'Of course 1 does not equal 2!');
});
test("This test will also fail", 1, function () {
// Will yield: "Expected 1 assertions, but 0 were run""
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment