JavaScript 自定义单元测试

时间:2021-11-24 02:55:47
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
'use strict';
var yyTester = (function(){
var testFunctions = [],
testDesc = []; function describe (desc, func) {
if (testFunctions.indexOf(func) < 0){
testFunctions.push(func);
testDesc.push(desc);
}
} function undescribe (func) {
var i = testFunctions.indexOf(func);
if (i >= 0){
testFunctions.splice(i, 1);
testDesc.splice(i, 1);
}
} function runTest(){
for (var i = 0, l = testFunctions.length; i < l; ++i){
console.log(testDesc[i]);
testFunctions[i]();
}
} return {
$describe : describe ,
$undescribe: undescribe,
$runTest: runTest
};
})(); (function(){
yyTester.$describe('数组测试', function(){
var a = [];
var aValue = 1;
a.push(aValue);
console.assert(a.length == 1);
console.assert(aValue == a[a.length - 1]);
console.assert(a.indexOf(aValue) == a.length - 1);
a.pop();
console.assert(a.length == 0);
}); yyTester.$runTest();
})();
</script>
</head>
<body>
</body>
</html>