Skip to content
Unit Testing

Introduction

Unit testing is a fundamental practice in software development where individual components or units of code are tested in isolation to ensure they work as intended. A unit test focuses on verifying the smallest testable parts of an application, typically individual functions, methods, or classes.

Key Benefits

  1. Early Bug Detection

    • Helps identify issues early in the development cycle
    • Reduces debugging time and costs
    • Prevents bugs from reaching production
  2. Code Quality

    • Enforces better code organization and modularity
    • Improves code maintainability
    • Acts as documentation for code behavior
  3. Refactoring Confidence

    • Allows safe refactoring of code
    • Provides immediate feedback on changes
    • Maintains system stability during updates
  4. Development Speed

    • Accelerates development by catching issues quickly
    • Reduces manual testing time
    • Enables continuous integration/deployment

Best Practices

  1. Test Independence

    • Each test should run independently
    • Avoid test interdependencies
    • Clean up test data after execution
  2. Clear Naming

    • Use descriptive test names
    • Follow a consistent naming convention
    • Indicate expected behavior in the name
  3. Single Responsibility

    • Test one thing per test case
    • Keep tests focused and specific
    • Follow the Arrange-Act-Assert pattern
  4. Maintainability

    • Keep tests simple and readable
    • Use helper methods for common setup
    • Update tests alongside code changes
Approach

Unit Test Approach

Unit test are typically written into three approach:

  1. Test-Driven Development (TDD)
    • Write tests before implementing code
    • Drive development through failing tests
    • Ensure code meets requirements
    • Write specific fuction or a piece of logic
  2. Standard Development Approach
    • Write tests after code implementation
    • Verify existing functionality
    • Focus on code coverage and quality
  3. Parallel Development Approach
    • Develop tests alongside code changes
    • Continuous validation of modifications
    • Maintain test coverage during updates
Test Coverage

Test Coverage

Test coverage is a measure of the extent to which your test suite exercises your code. It helps you understand how much of your code is being tested and identify areas that may need additional testing.

Types of Coverage

  1. Statement Coverage
    • Measures the number of executable statements that are executed during testing.
    • It is a basic level of coverage that checks if every executable line of code is executed at least once.
    • It is useful for ensuring that the core logic of the code is being tested.
    • Example:
      javascript
      function add(a, b) {
          return a + b;
      }
  2. Branch Coverage
    • Measures the number of branches (conditional statements) that are executed during testing.
    • It is useful for ensuring that different paths through the code are being tested.
    • Example:
      javascript
      function checkSign(n) {
          if (n > 0) {
              return "positive";
          } else {
              return "negative";
          }
      }
  3. Function Coverage
    • Measures the number of functions that are executed during testing.
    • It is useful for ensuring that all functions are being tested.
    • Example:
      javascript
      function add(a, b) {
          return a + b;
      }
  4. Line Coverage
    • Measures the number of lines of code that are executed during testing.
    • It is useful for ensuring that the code is being tested at a detailed level.
    • Example:
      javascript
      function checkEvenOdd(n) {
          if (n % 2 === 0) { 
              return "even"; // Line 2    
          } else { 
              return "odd"; // Line 4
          }
      }