Lecture 37: Unit Testing
Unit 5: Software Coding and Testing (4353202)
Lecture Agenda
- Recap of Software Documentation
- What is Software Testing?
- Levels of Software Testing
- What is Unit Testing?
- Why is Unit Testing Important?
- Unit Testing Process
- Tools for Unit Testing
- Key Takeaways
Recap of Software Documentation
Software documentation includes internal (code comments, API docs) and external (user manuals, installation guides) materials, crucial for knowledge transfer, maintenance, and user support.
What is Software Testing?
Software Testing is the process of evaluating a software system or component to determine whether it satisfies specified requirements, identifies defects, and ensures quality.
It is a critical phase in the SDLC that helps in delivering a reliable and robust product.
Levels of Software Testing
Software testing is typically performed at different levels, each focusing on a specific aspect of the system:
- Unit Testing: Testing individual components or modules.
- Integration Testing: Testing the interaction between integrated modules.
- System Testing: Testing the complete and integrated software system.
- Acceptance Testing: Testing the system for acceptability by the user.
What is Unit Testing?
Unit Testing is the first level of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software performs as designed.
A unit is the smallest testable part of any software. It could be a function, method, procedure, module, or object.
Why is Unit Testing Important?
- Early Bug Detection: Catches bugs early in the development cycle, reducing the cost of fixing them.
- Facilitates Change: Makes it safer to refactor code or add new features, as tests can quickly identify regressions.
- Simplifies Integration: Reduces the number of defects found during integration testing.
- Provides Documentation: Unit tests serve as living documentation of the code's functionality.
- Improves Code Quality: Encourages developers to write modular, testable code.
Unit Testing Process
- Identify Units: Determine the smallest testable parts of the application.
- Create Test Cases: For each unit, define inputs, expected outputs, and conditions.
- Write Test Code: Use a unit testing framework to write automated tests.
- Run Tests: Execute the unit tests.
- Analyze Results: Check if tests pass or fail. If a test fails, debug and fix the code.
- Refactor: Improve the code quality while ensuring all tests still pass.
Tools for Unit Testing
- Java: JUnit, TestNG
- Python: unittest, pytest
- JavaScript: Jest, Mocha, Jasmine
- C#: NUnit, MSTest
- PHP: PHPUnit
These frameworks provide assertions, test runners, and reporting capabilities to automate unit testing.
Key Takeaways
- **Unit Testing** is the first level of testing, focusing on individual code components.
- It is crucial for **early bug detection** and **improving code quality**.
- Unit tests act as **safety nets** for refactoring and new feature development.
- Automated **unit testing frameworks** are widely used across programming languages.
Next Lecture
Topic: Black Box Testing - Equivalence Partitioning
Q & A
Questions & Discussion

