QUnit - Using Assertions



All the assertions are in the Assert Category.

This category provides a set of assertion methods useful for writing tests. Only failed assertions are recorded.

Sr.No. Methods & Description
1

async()

Instruct QUnit to wait for an asynchronous operation.

2

deepEqual()

A deep recursive comparison, working on primitive types, arrays, objects, regular expressions, dates, and functions.

3

equal()

A non-strict comparison, roughly equivalent to JUnit's assertEquals.

4

expect()

Specify how many assertions are expected to run within a test.

5

notDeepEqual()

An inverted deep recursive comparison, working on primitive types, arrays, objects, regular expressions, dates, and functions.

6

notEqual()

A non-strict comparison, checking for inequality.

7

notOk()

A boolean check, inverse of ok() and CommonJS's assert.ok(), and equivalent to JUnit's assertFalse(). Passes if the first argument is false.

8

notPropEqual()

A strict comparison of an object's own properties, checking for inequality.

9

notStrictEqual()

A strict comparison, checking for inequality.

10

ok()

A boolean check, equivalent to CommonJS's assert.ok() and JUnit's assertTrue(). Passes if the first argument is true.

11

propEqual()

A strict type and value comparison of an object's own properties.

12

push()

Report the result of a custom assertion.

13

strictEqual()

A strict type and value comparison.

14

throws()

Test if a callback throws an exception, and optionally compare the thrown error.

Let's try to cover most of the above mentioned methods in an example.

<html>
   <head>
      <meta charset = "utf-8">
      <title>QUnit basic example</title>
      <link rel = "stylesheet" href = "https://code.jquery.com/qunit/qunit-1.22.0.css">
      <script src = "https://code.jquery.com/qunit/qunit-1.22.0.js"></script>
   </head>
   
   <body>
      <div id = "qunit"></div>
      <div id = "qunit-fixture"></div> 
      <script>
         QUnit.test( "TestSuite", function( assert ) {
            //test data
            var str1 = "abc";
            var str2 = "abc";
            var str3 = null;
            var val1 = 5;
            var val2 = 6;
            var expectedArray = ["one", "two", "three"];
            var resultArray =  ["one", "two", "three"];

            //Check that two objects are equal
            assert.equal(str1, str2, "Strings passed are equal.");
			
            //Check that two objects are not equal
            assert.notEqual(str1,str3, "Strings passed are not equal.");

            //Check that a condition is true
            assert.ok(val1 < val2, val1 + " is less than " + val2);
			
            //Check that a condition is false
            assert.notOk(val1 > val2, val2 + " is not less than " + val1);

            //Check whether two arrays are equal to each other.
            assert.deepEqual(expectedArray, resultArray ,"Arrays passed are equal.");
			
            //Check whether two arrays are equal to each other.
            assert.notDeepEqual(expectedArray, ["one", "two"],
               "Arrays passed are not equal.");			
         });
      </script>
   </body>
</html>

Verify the Output

You should see the following result −

Advertisements