|
ECMAScript/JavaScript Unit Testing
Using QUnit |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
equal(actual,expected [, message])
deepEqual(actual,expected [, message])
throws()
== or ===
approxEquals() function that returns
a boolean and use the ok() assertion
toFixed() method
test("Underweight Test", function() {
equal(calculateBMI(123, 69).toFixed(1), 18.2);
});
/**
* A function to calculate and display the body mass index (BMI)
*
* @param {number} weight - The weight in pounds
* @param {number} height - The height in pounds
*/
function calculateBMI(weight, height)
{
"use strict"; // ECMAScript 5
var bmi;
bmi = weight / (height * height) * 703;
if (bmi < 15){
bmi = 15;
}else if (bmi > 60){
bmi = 60;
}
return bmi;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>BMI Test Suite</title>
<link rel="stylesheet" href="QUnit/qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="QUnit/qunit.js"></script>
<script src="../clientsidebasics/bmi.js"></script>
<script src="bmi-test.js"></script>
</body>
</html>
test("Underweight Tests", function() {
equal(calculateBMI(145, 75).toFixed(0), 18);
equal(calculateBMI(110, 69).toFixed(0), 16);
});
test("Normal Tests", function() {
equal(calculateBMI( 91, 58).toFixed(0), 19);
equal(calculateBMI( 96, 58).toFixed(0), 20);
equal(calculateBMI(115, 58).toFixed(0), 24);
equal(calculateBMI(104, 62).toFixed(0), 19);
equal(calculateBMI(115, 62).toFixed(0), 21);
equal(calculateBMI(180, 76).toFixed(0), 22);
equal(calculateBMI(197, 76).toFixed(0), 24);
});
test("Overweight Tests", function() {
equal(calculateBMI(150, 65).toFixed(0), 25);
equal(calculateBMI(173, 66).toFixed(0), 28);
equal(calculateBMI(173, 66).toFixed(0), 32, "Intentional Failure");
equal(calculateBMI(238, 76).toFixed(0), 29);
});
module() to group testsmodule()
module("Underweight Tests");
test("UW", function() {
equal(calculateBMI(145, 75).toFixed(0), 18);
});
test("UW", function() {
equal(calculateBMI(110, 69).toFixed(0), 16);
});
module("Normal Tests");
test("N", function() {
equal(calculateBMI( 91, 58).toFixed(0), 19);
});
test("N", function() {
equal(calculateBMI( 96, 58).toFixed(0), 20);
});
test("N", function() {
equal(calculateBMI(115, 58).toFixed(0), 24);
});
test("N", function() {
equal(calculateBMI(104, 62).toFixed(0), 19);
});
test("N", function() {
equal(calculateBMI(115, 62).toFixed(0), 21);
});
test("N", function() {
equal(calculateBMI(180, 76).toFixed(0), 22);
});
test("N", function() {
equal(calculateBMI(197, 76).toFixed(0), 24);
});
module("Overweight Tests");
test("OW", function() {
equal(calculateBMI(150, 65).toFixed(0), 25);
});
test("OW", function() {
equal(calculateBMI(173, 66).toFixed(0), 28);
});
test("OW", function() {
equal(calculateBMI(173, 66).toFixed(0), 32, "Intentional Failure");
});
test("OW", function() {
equal(calculateBMI(238, 76).toFixed(0), 29);
});
module()
module():
module() function has an optional parameter
that can be used to pass a setup and
teardown function