Created
October 9, 2020 15:05
-
-
Save tigercosmos/8fa42d99e640a2eca70eb64d80e0410a to your computer and use it in GitHub Desktop.
Simple Test Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator { | |
constructor() { | |
} | |
add(a, b) { | |
return a + b; | |
} | |
sub(a, b) { | |
return a - b; | |
} | |
mul(a, b) { | |
return a * b; | |
} | |
div(a, b) { | |
if( b == 0) { | |
throw Error("Cannot be zero!"); | |
} | |
return a / b; | |
} | |
} | |
module.exports = Calculator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: $ node test.js | |
const Calculator = require("./Calculator"); | |
const assert = require("assert"); | |
const cal = new Calculator(); | |
function test_add() { | |
for (let i = 0; i < 100; i++) { | |
const a = Math.random(); | |
const b = Math.random(); | |
assert.strictEqual(cal.add(a, b), a + b); | |
} | |
console.log("ADD PASS!!!!"); | |
} | |
function test_sub() { | |
for (let i = 0; i < 100; i++) { | |
const a = Math.random(); | |
const b = Math.random(); | |
assert.strictEqual(cal.sub(a, b), a - b); | |
} | |
console.log("SUB PASS!!!!"); | |
} | |
function test_mul() { | |
for (let i = 0; i < 100; i++) { | |
const a = Math.random(); | |
const b = Math.random(); | |
assert.strictEqual(cal.mul(a, b), a * b); | |
} | |
console.log("MUL PASS!!!!"); | |
} | |
function test_div() { | |
for (let i = 0; i < 100; i++) { | |
const a = Math.random(); | |
const b = Math.random(); | |
assert.strictEqual(cal.div(a, b), a / b); | |
assert.throws(() => { | |
cal.div(a, 0); | |
}); | |
} | |
console.log("DIV PASS!!!!"); | |
} | |
function test_main() { | |
test_add(); | |
test_sub(); | |
test_mul(); | |
test_div(); | |
console.log("ALL PASS!!!!"); | |
} | |
test_main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is for the Youtube video tutorial: 如何寫程式測試