Skip to content

Commit eca9398

Browse files
author
Ogbemi mene
committed
2**
1 parent 45eed61 commit eca9398

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# A Beginner's Guide to Testing Functions
2+
3+
## 1. What Is a Function?
4+
5+
```
6+
Input ──▶ Function ──▶ Output
7+
```
8+
9+
A function
10+
- Takes **input** (via **arguments**)
11+
- Does some work
12+
- Produces **one output** (via a **return value**)
13+
14+
Example:
15+
16+
```
17+
sum(2, 3) → 5
18+
```
19+
20+
Important idea: the same input should produce the same output.
21+
22+
23+
## 2. Testing Means Predicting
24+
25+
Testing means:
26+
> If I give this input, what output should I get?
27+
28+
29+
## 3. Choosing Good Test Values
30+
31+
### Step 1: Determining the space of possible inputs
32+
Ask:
33+
- What type of value is expected?
34+
- What values make sense?
35+
- If they are numbers:
36+
- Are they integers or floating-point numbers?
37+
- What is their range?
38+
- If they are strings:
39+
- What are their length and patterns?
40+
- What values would not make sense?
41+
42+
### Step 2: Choosing Good Test Values
43+
44+
#### Normal Cases
45+
46+
These confirm that the function works in normal use.
47+
48+
- What does a typical, ordinary input look like?
49+
- Are there multiple ordinary groups of inputs? e.g. for an age checking function, maybe there are "adults" and "children" as expected ordinary groups of inputs.
50+
51+
52+
#### Boundary Cases
53+
54+
Test values exactly at, just inside, and just outside defined ranges.
55+
These values are where logic breaks most often.
56+
57+
#### Consider All Outcomes
58+
59+
Every outcome must be reached by at least one test.
60+
61+
- How many different results can this function produce?
62+
- Have I tested a value that leads to each one?
63+
64+
#### Crossing the Edges and Invalid Values
65+
66+
This tests how the function behaves when assumptions are violated.
67+
- What happens when input is outside of the expected range?
68+
- What happens when input is not of the expected type?
69+
- What happens when input is not in the expected format?
70+
71+
## 4. How to Test
72+
73+
### 1. Using `console.assert()`
74+
75+
```javascript
76+
// Report a failure only when the first argument is false
77+
console.assert( sum(4, 6) === 10, "Expected 4 + 6 to equal 10" );
78+
```
79+
80+
It is simpler than using `if-else` and requires no setup.
81+
82+
### 2. Jest Testing Framework
83+
84+
```javascript
85+
test("Should correctly return the sum of two positive numbers", () => {
86+
expect( sum(4, 6) ).toEqual(10);
87+
... // Can test multiple samples
88+
});
89+
90+
```
91+
92+
Jest supports many useful functions for testing but requires additional setup.

0 commit comments

Comments
 (0)