天天看點

使用Qunit對js代碼進行單元測試

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>qunit example</title>

<link rel="stylesheet" href="//code.jquery.com/qunit/qunit-1.15.0.css">

</head>

<body>

<div id="qunit"></div>

<div id="qunit-fixture"></div>

<script src="//code.jquery.com/qunit/qunit-1.15.0.js"></script>

<script src="project.js"></script>

<script src="tests.js"></script>

</body>

</html>

  最後面引入的 project.js 就是待測試的檔案

  2、測試用例的編寫

  先寫一個待測試例子,這是一個判斷是否是偶數的方法

//project.js

function iseven(val) {

return val % 2 === 0;

}

  3、編寫測試

//tests.js<br>test('iseven()', function() {

ok(iseven(0), 'zero is an even number');

ok(iseven(2), 'so is two');

ok(iseven(-4), 'so is negative four');

ok(!iseven(1), 'one is not an even number');

ok(!iseven(-7), 'neither is negative seven');

})

最新内容請見作者的github頁:http://qaseven.github.io/

繼續閱讀