Closed phuanh004 closed 1 year ago
I asked ChatGPT about how to write unit tests without using an external framework. Here is what it said:
I asked ChatGPT about how to write unit tests without using an external framework. Here is what it said:
That's cool. But make sure you guys have nodejs
on ur laptop/ pc.
I think we need to install node.js for Lab 4 anyways.
The example above could be done using GitHub action.
// Create a file named test.js
// Define a simple add function
function add(a, b) {
return a + b;
}
// Define a simple test function
function test(name, fn) {
try {
fn();
console.log(`✅ ${name}`);
} catch (error) {
console.error(`❌ ${name}`);
console.error(error);
process.exitCode = 1;
}
}
// Write a test case for the add function
test('add(1, 2) should return 3', () => {
const actual = add(1, 2);
const expected = 3;
if (actual !== expected) {
throw new Error(`Expected ${expected}, but got ${actual}`);
}
});
This code snippet is adapted from https://github.com/remarkablemark/github-actions-nodejs/blob/master/test.js. You can run this test with GitHub actions by creating a file named .github/workflows/test.yml with the following content:
name: Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: node test.js
Possible unittest:
assertTrue
if inputIs this a valid question?
assertFalse
if inputThis is not a question
getRandomAnswer
will return a string