TDD ‐ Test Driven Development - JUCSE49-Mavericks/Smart-Class-Routine-Management-System GitHub Wiki
Test Driven Development (TDD) Report for “Smart Class Routine Management System”
Author
: Akila Nipo
📚 Introduction
Test Driven Development (TDD) is a software development methodology where test cases are written before the actual code that fulfills them. It follows a short, repetitive development cycle to ensure correctness and precise design.
The main steps include:
➡️ 1. Add a Test: Write a test case based on function requirements.
➡️ 2. Run Tests: Confirm that the new test fails.
➡️ 3. Write Code: Develop the code required to pass the test.
➡️ 4. Run Tests Again: Ensure all tests pass.
➡️ 5. Refactor Code: Remove redundancy and optimize.
➡️ 6. Repeat: Continue the cycle to build up functionality.
🔁 TDD focuses on improving the design of code while ensuring that functionality is always tested through iteration.
📜 TDD vs. Unit Testing
Aspect | TDD | Unit Testing |
---|---|---|
Purpose | Drives design by ensuring tests are in place before coding | Validates individual units of code after implementation |
Development Cycle | Test-first approach with continuous refactoring | Test-after approach with static test cases |
Code Reliability | Ensures high reliability through iterative testing | Primarily confirms correctness without guiding design |
Ideal Use Cases | Complex, evolving projects needing strong design alignment | Smaller projects or specific features within larger projects |
⚡ Summary
- TDD: Ideal for projects requiring robust code design and maintainability.
- Unit Testing: Suitable for simple validation tasks and focused testing of specific sections.
🔧 Pre-requisites
➡️ Tools: Visual Studio Code
➡️ Framework: Node.js with Express
➡️ Unit Testing Tool: Mocha (with Chai for assertions)
➡️ Test Coverage Tool: npm install nyc --save-dev
📋 Procedure
-
Setup Mocha: Create a
test
folder and add the test file (e.g.,syllabusFilter.test.js
). -
Write Initial Test Cases: Design tests to verify each function’s expected behavior.
Unit Testing Guide -
Run Initial Tests: Execute Mocha tests:
mocha test/syllabusFilter.test.js
-
Implement Code: Write code to meet failing test cases.
-
Refactor and Repeat: Optimize code to ensure efficiency.
🚨 Test Case Fails
Example Feature: Filter Syllabus
-
Initial Test Case Failure
-
Feature: Filtering Syllabus
-
Observation: Out of 8 test cases, all of them failed due to missing conditions for fetching syllabus.
At the time of failure, the function to filter syllabus was:
✅ Test Case Passes
Resolution
-
Completed Function Logic: The logic for fetching the syllabus was implemented successfully, passing all tests.
-
Verification: We checked the function’s performance for the test case with the following command:
mocha test/syllabusFilter.test.js
📊 Results
Test Coverage Report
To generate a test coverage report, run the following commands:
npm install nyc --save-dev
npx nyc report --reporter=text
📊 Report Analysis
✅ Statement Coverage: 93.24% – Most lines are tested, with a few uncovered lines.
⚙️ Branch Coverage: 75%
✔️ Function Coverage: 100% – All functions in the file are covered by tests.
📜 Line Coverage: 100% – All lines of code are executed during tests, except for a few uncovered lines mentioned below.
🔍 Uncovered Lines:
- Lines: 45, 62, 79, 106, 182