Introduction to Testing
"Program testing can show the presence of bugs, but never their absence" โ Dijkstra.
- Software Testing = ุฅูู ุชุดุบู ุงูุจุฑูุงู ุฌ ุจุบุฑุถ ุฅูู ุชูุงูู ุงูู errors ุงููู ููู.
- ูู ุนู ููุฉ ุจุชุฏูู ุซูุฉ ุฅู ุงูุจุฑูุงู ุฌ ุจูุนู ู ุงูู ุทููุจ ู ูู ุจุงูุธุจุท ุนู ุทุฑูู ุฅูู ุชุฌุฑุจู ุนูู ุฏุงุชุง ู ุฎุชููุฉ (test data).
- ูุดุงุท ุฎุงุต ุจุงูู QA ูุฏูู ุฅูู ููุงูู ุงูู bugs ูู ุงูููุฏ ุงูุดุบุงู ููุตูุญูุง.
- Tester: ูู ุงููู ุจููุชุจ ุงูู test cases ููุดุบู ุงูุจุฑูุงู ุฌ ุนุดุงู ููุงูู ุงูู bugs.
- QA Engineer: ูู ุงููู ุจูุฎุทุท ููู QA ูุจูุญุท standards ูุทุฑู ุนุดุงู ูู ูุน ุงูู bugs ุฅููุง ุชุญุตู ู ู ุงูุฃุณุงุณ.
Bugs โ Cost & Types
Cost of bug grows exponentially as it's found later.
| Phase | Cost to Fix |
|---|---|
| Specification | $1 |
| Design | $10 |
| Code | $100 |
| Test | $1,000 |
| Release | $1,000+ |
Goal: ุชูุงูู ุงูู bugs ุจุฏุฑู ุนูู ูุฏ ู ุง ุชูุฏุฑ (ุนุดุงู ุงูุชูููุฉ ู ุชุฒูุฏุด).
ุงู ุชู ูููู ุฅู ููู bugุ ูู ุญุตู ุญุงุฌุฉ ุฃู ุฃูุชุฑ ู ู ุฏูู:
- Rule 1: ุงูุณููุชููุฑ ู ุจูุนู ูุด ุญุงุฌุฉ ุงูู spec ูุงู ุฅูู ุงูู ูุฑูุถ ูุนู ููุง.
- Rule 2: ุงูุณููุชููุฑ ุจูุนู ู ุญุงุฌุฉ ุงูู spec ูุงู ุฅูู ุงูู ูุฑูุถ ู ูุนู ููุงุด.
- Rule 3: ุงูุณููุชููุฑ ุจูุนู ู ุญุงุฌุฉ ุงูู spec ู ุฌุงุจุด ุณูุฑุชูุง ุฎุงูุต.
- Rule 4: ุงูุณููุชููุฑ ู ุจูุนู ูุด ุญุงุฌุฉ ุงูู spec ู ุฌุงุจุด ุณูุฑุชูุง ุจุณ ุงูู ูุฑูุถ ุจุฏูููุงู ุชุชุนู ู.
- Rule 5: ุงูุณููุชููุฑ ุตุนุจ ูุชููู ุ ุฑุฎู ูู ุงุณุชุฎุฏุงู ูุ ุฃู ุจุทูุก... ูุนูู ู ุด ู ุธุจูุท.
- Immediate fix โ ูุงุฒู ุชุชุตูุญ ููุฑุงู ูุฃููุง ุจุชุนุทู ุงูู testing ููุงุถุญุฉ ุฌุฏุงู.
- ูุงุฒู ุชุชุตูุญ ูุจู ุงูู release.
- ูุณุชุญุณู ุชุชุตูุญ ูู ุง ูููู ููู ููุช.
- ูุง ุฑูุช ุชุชุตูุญ ุจุณ ูููุน ููุฒู ุจุงูู release ุนุงุฏู ู ู ุบูุฑูุง.
Testing Levels (Stages)
4 ู ุณุชููุงุช ุจุชุจุฏุฃ ู ู ุงูู unit ูุญุฏ ุงูู acceptance.
| Level | Purpose |
|---|---|
| Unit (UT) | Test individual components; each unit performs as designed |
| Integration (IT) | Test groups of units to expose interaction faults |
| System (ST) | Complete integrated system vs specified requirements |
| Acceptance (AT) | Business requirements + delivery readiness |
Black-Box vs White-Box
ุฎุฏ ุจุงููุ ุงู ุชุญุงู ุงูุดุชุง ุจุชุงุน 2025 ุณุฃู ุนู ุงูู Black-box equivalence partitioning.
- ุจูุชุฃูุฏ ุฅู ุงูุจุฑูุงู ุฌ ู ุงุดู ู ุน ุงูู specification ุงูู ูุชูุจุฉ โ (function coverage).
- Abstraction from details: ู ุด ู ุญุชุงุฌ ุชุดูู ุงูู source code ุฃุตูุงู.
- Scales up: ุจูููุน ู ุน ูู ุงูู ุณุชููุงุช (unit, integration, system, acceptance).
- ุจููุดู ุงูู missing functionality (ุงูุญุงุฌุงุช ุงููู ูุงูุตุฉ ูู ุด ู ูุฌูุฏุฉุ ูุฏู ุงูู white-box ู ูุนุฑูุด ูุนู ูู).
- ู ู ุทุฑูู: Equivalence Partitioning ูุงูู Boundary Value Analysis.
- ุจูุชูุณุช ุนูู ุงูู implementation ููุณู (ุงูููุฏ).
- ุงูู tester ุจูููู ู ุชุฃูุฏ ู ู ุงูู code coverage (ุบุทู ูุงู ุณุทุฑ).
- ู ุจูู ุนูู ู ุณุงุฑ ุงูุฏุงุชุง ูุงูููุชุฑูู ูู ุงูููุฏ โ ูุจูุณููู ุงูู debugging.
- ู ุจูููุนุด ููุจุฑ ู ุนุงู ุฃูู (Does NOT scale up) โ ุจูููุฏ ุฃูุชุฑ ุญุงุฌุฉ ูู ุงูู unit ูุงูู integration.
- ุฃููุงุน ุงูู Coverage ููู: Statement, Branch, Path.
- ุจููุดู ุงูู unexpected functionality (ุงูุญุงุฌุงุช ุงููู ุงูููุฏ ุจูุนู ููุง ุจุงูุบูุท ุฃู ุฒูุงุฏุฉ).
ุจุชูุณู ุงูู inputs ุงููู ุนูุฏู ูู ู ุฌู ูุนุชูู ุฃู ุฃูุชุฑ:
- ู ุฌู ูุนุฉ ุงูู Valid (ุงูุตุญ ุงููู ุงูู ูุฑูุถ ูุชูุจู)
- ู ุฌู ูุนุฉ ุงูู Invalid (ุงูุบูุท ุงููู ุงูู ูุฑูุถ ูุชุฑูุถ)
ูุจุนุฏูู ุชุงุฎุฏ ููู ุฉ ูุงุญุฏุฉ ู ู ูู ู ุฌู ูุนุฉ ุชุฌุฑุจ ุจููุง (ูู ู ูุฏูุจ ุนู ุงูุจุงูู).
Example โ Reward Points (Winter 2025 Q1.04)
Function returns reward points. If purchase โค 1000 LE โ 10%. If > 1000 LE โ 20%. If input is negative โ throw exception.
Partitions:
- Input โค 0 โ exception
- Input (0, 1000] โ 10% reward
- Input > 1000 โ 20% reward
Test cases to cover all partitions:
TC1: input 100, output 10 (10% partition)
TC2: input 1000, output 100 (10% partition โ boundary)
TC3: input -100, throw exception (invalid)
// But this misses the 20% partition!
// Correct set:
TC1: 10 & M, 18 & F, 75 & F // (Winter 2025 example)
TC2: 100 โ 10, 2000 โ 400, -200 โ throw
ุฃุบูุจ ุงูู bugs ุจุชุจูู ู ูุฌูุฏุฉ ุนูุฏ ุงูู boundaries (ุงูุญุฏูุฏ). ุนุดุงู ูุฏู ุฌุฑุจ ููู ุฉ ุนูู ุงูุญุฏ ุจุงูุธุจุทุ ูุฃูู ู ูู ุจุณูุฉุ ูุฃูุจุฑ ู ูู ุจุณูุฉ.
ู ุซุงู ูู ุงูุนู ุฑ ู ู 18 ูู 65:
- 17, 18, 19 (ุงูุญุฏ ุงููู ุชุญุช)
- 64, 65, 66 (ุงูุญุฏ ุงููู ููู)
- -1, 0, 1 (ุญุงูุงุช ุฎุงุตุฉ ุนูุฏ ุงูุตูุฑ)
Test-Driven Development (TDD) โญ
ุฌู ูู ุงู ุชุญุงู ุงูุดุชุง ุจุชุงุน 2025 ุณุคุงู ุนู ุชุฑุชูุจ ุงูู TDD steps.
ุงูู TDD ูู ุฃุณููุจ ูู ุงูุดุบู ุจูุฑูุฒ ููู ุฅููุง ููุชุจ ุงูู unit test cases ุงูุฃูู ูุจู ู ุง ููุชุจ ููุฏ ุงูุจุฑูุงู ุฌ ููุณู.
ุดุบุงููู ุจูุธุงู iterative (ููุงุช) ุจุชุฌู ุน ุจูู: ูุชุงุจุฉ ุงูููุฏ + ุงูู unit tests + ุชุธุจูุท ุงูููุฏ (refactoring).
1. RED
ุงูุชุจ ุชุณุช ุจูููู
2. GREEN
ุงูุชุจ ููุฏ ูู ุดูู ุงูุชุณุช
3. REFACTOR
ูุถูู ุงูููุฏ ู ู ุบูุฑ ู ุง ุชูุณุฑู
- ู ู ููุน ุชูุชุจ ุฃู production code ูุจู ู ุง ุชูุชุจ ุชุณุช ุจูููู (failing test).
- ู ู ููุน ุชูุชุจ ููุฏ ูู ุงูุชุณุช ุฃูุชุฑ ู ู ุงููู ู ุญุชุงุฌู ุนุดุงู ูููู.
- ู ู ููุน ุชูุชุจ production code ุฃูุชุฑ ู ู ุงููู ู ุญุชุงุฌู ุนุดุงู ุชูุฌุญ ุงูุชุณุช ุงููู ุจูููู ุฏูููุชู.
ุชุฑุชูุจ ุฎุทูุงุช ุงูู TDD ุงูุตุญ ูุงูุชุงูู (ุฏู ุณุคุงู ุฌู ูู ุงู ุชุญุงู ุงูุดุชุง):
- ุงูุชุจ ุชุณุช (Write a test) (ุฎุทูุฉ 2 ูู ูุฑูุฉ ุงูุฃุณุฆูุฉ)
- ุงูุชุณุช ูููู (Test fails) (ุฎุทูุฉ 3)
- ุงูุชุจ ุงูููุฏ (Write code) (ุฎุทูุฉ 5)
- ุงูุชุณุช ููุฌุญ (Test passes) (ุฎุทูุฉ 4)
- ูุถู ุงูููุฏ (Refactor code) (ุฎุทูุฉ 1)
2 โ 3 โ 5 โ 4 โ 1 = (c) in the exam.// ๐ด Step 1: Write failing test
function testAbsoPos5() {
return abso(5) === 5;
}
// ๐ข Step 2: Write minimal code
function abso(num) {
return num; // Just enough to pass
}
// ๐ด Step 3: Write new failing test
function testAbsoNeg5() {
return abso(-5) === 5;
}
// ๐ข Step 4: Extend code to pass
function abso(num) {
if (num < 0) return -num;
return num;
}
// ๐ต Step 5: Refactor (if needed)
Python unittest Library
ุงูู standard library ูู ุจุงูุซูู ุนุดุงู ุชุนู ู unit testing.
- ุจุชุนู
ู
import unittest. - ุชุนู
ู Import ููููุฏ ุจุชุงุนู ุงููู ูุชุนู
ูู ุชุณุช:
from my_module import my_function. - ุชุนู
ู ููุงุณ ููุชุณุช:
class TestMyCode(unittest.TestCase). - ุชูุชุจ ุงูู test methods ุจุชุงุนุชู ููุงุฒู
ุชุจุฏุฃ ุจููู
ุฉ
test_.
import unittest
class TestSomething(unittest.TestCase):
def test_pass(self):
self.assertEqual(2 + 2, 4)
def test_fail(self):
self.assertEqual(2 + 2, 42)
if __name__ == '__main__':
unittest.main()
| Method | Use |
|---|---|
assertEqual(a, b) |
Passes if a == b |
assertNotEqual(a, b) |
Passes if a != b |
assertTrue(x) |
Passes if x is truthy |
assertFalse(x) |
Passes if x is falsy |
assertIsNone(x) |
Passes if x is None |
assertIsNotNone(x) |
Passes if x is not None |
assertIn(a, list) |
Passes if a is in list |
assertAlmostEqual(a, b) |
For floats (handles tiny diffs) |
REST APIs โญ
Winter 2025 Q1.02: REST APIs โ which is FALSE?
- API (Application Programming Interface) = set of rules to interact with another software.
- REST (Representational State Transfer) = architectural style for web services serving data (not web pages).
- Communicate over HTTP using standard methods.
- Looks like a website BUT it's NOT a website. Has an endpoint.
- Serves data, not web pages. Accessed by software.
| Method | Example |
|---|---|
| GET /users | Fetches list of users |
| POST /users | Adds a new user |
| PUT /users/1 | Updates user with ID 1 |
| DELETE /users/1 | Deletes user with ID 1 |
- Resources: objects/data the API manipulates (users, posts).
- URIs (Uniform Resource Identifiers): unique endpoint per resource
(
/users/1). - HTTP Methods: GET, POST, PUT, DELETE.
- Stateless: each request must contain ALL information needed โ no session state on server. โญ
- Responses: status codes + data in JSON or XML.
HTTP Status Codes
| Code | Meaning |
|---|---|
| 200 OK | Successful GET |
| 201 Created | Successful POST |
| 404 Not Found | Resource doesn't exist |
import requests
url = "https://catfact.ninja/fact"
try:
response = requests.get(url)
response.raise_for_status() # Raise on HTTP error
data = response.json()
print(data)
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Exam Bank โ Lecture 16
4 ุฃุณุฆูุฉ ู ู Winter 2025 + 4 practice.
1) refactor code ยท 2) write a test ยท 3) test fails ยท 4) test passes ยท 5) write code
- 200 โ 20 (ุฏู ุจุชุบุทู ุงูุฌุฒุก ุจุชุงุน ุงูู 10% ููุฃุฑูุงู โค1000)
- 2000 โ 400 (ุฏู ุจุชุบุทู ุงูุฌุฒุก ุจุชุงุน ุงูู 20% ููุฃุฑูุงู >1000)
- โ200 โ exception (ุฏู ุจุชุบุทู ุงูุฌุฒุก ุงูู invalid)