<script> functionpow(x, n) { if (n === 0) { return1; } else { return x * pow(x, n - 1); } } </script>
加入进行测试的js文件
1
<scriptsrc="test.js"></script>
创建mocha元素
1
<divid="mocha"></div>
执行mocha得到结果
1 2 3
<script> mocha.run(); </script>
关于测试文件的编写
demo代码如下:
编写单个测试
1 2 3 4 5
describe("pow", function() { it(`res is ${pow(2,3)} or not 8`, function() { assert.equal(pow(2, 3), 8); }); });
编写多个测试
1 2 3 4 5 6 7 8 9 10 11
describe("pow", function () { it(`res is ${pow(2, 3)} or not 8`, function () { assert.equal(pow(2, 3), 8); }); it(`res is ${pow(2, 4)} or not 16`, function () { assert.equal(pow(2, 4), 16); }); it(`res is ${pow(2, 5)} or not 32`, function () { assert.equal(pow(2, 5), 32); }); });
进行指定测试执行
1 2 3 4 5 6 7 8 9 10 11
describe("pow", function () { it(`res is ${pow(2, 3)} or not 8`, function () { assert.equal(pow(2, 3), 8); }); it.only(`res is ${pow(2, 4)} or not 16`, function () { assert.equal(pow(2, 4), 16); }); it(`res is ${pow(2, 5)} or not 32`, function () { assert.equal(pow(2, 5), 32); }); });