Skip to content

Instantly share code, notes, and snippets.

@tigercosmos
Created October 9, 2020 15:05
Show Gist options
  • Save tigercosmos/8fa42d99e640a2eca70eb64d80e0410a to your computer and use it in GitHub Desktop.
Save tigercosmos/8fa42d99e640a2eca70eb64d80e0410a to your computer and use it in GitHub Desktop.
Simple Test Tutorial
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;
// 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();
@tigercosmos
Copy link
Author

This code is for the Youtube video tutorial: 如何寫程式測試

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment