Software Engineering · Unit 6
Software Testing
Exam-focused notes for Software Testing (Software Engineering, BIT302): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.
Software Testing in short
Testing checks that software does what it should and finds faults before users do. Testing can show that faults are present, but it can never prove that there are none.
What this unit covers
- Types of testing
- Black box testing
- White box testing
- Unit testing
- Component testing
- Integration testing
- System testing
- Release testing
- Test-driven development approach
Test-driven development approach
2082 · 10 marks
Explain the test driven development. List its advantages and disadvantages.[10]
Show solved answerHide answer
Test Driven Development (TDD)
Note: No specific reference notes were found for this topic. The following answer is based on standard Software Engineering curriculum content appropriate for BSc CSIT, Tribhuvan University.
Definition
Test Driven Development (TDD) is a software development approach in which test cases are written before the actual code is written. The developer first writes a failing test, then writes the minimum amount of code to pass that test, and finally refactors the code. This cycle is repeated throughout the development process.
TDD follows the principle: "Write tests first, then write code to satisfy those tests."
The TDD Cycle (Red-Green-Refactor)
The core process of TDD follows three repeating steps:
1. RED --> Write a test that FAILS (feature not yet implemented)
2. GREEN --> Write just enough code to PASS the test
3. REFACTOR --> Clean up the code without breaking the test
This cycle is also known as the "Red-Green-Refactor" cycle.
Steps in TDD Process
-
Add a Test
- Before writing any functional code, write a test case for the new feature or function.
- The test will fail initially because the feature does not exist yet.
-
Run All Tests
- Run the test suite to confirm the new test fails (Red phase).
- This verifies the test is valid and the feature is truly missing.
-
Write the Code
- Write the minimum amount of code necessary to make the failing test pass.
- Do not write extra or unnecessary code at this stage.
-
Run Tests Again
- Run all tests to confirm the new test now passes (Green phase).
- Ensure no existing tests are broken.
-
Refactor the Code
- Clean up the code: remove duplication, improve readability, optimize structure.
- Run tests again to ensure refactoring did not break anything.
-
Repeat
- Repeat the cycle for the next feature or requirement.
Diagram of TDD Cycle
+------------------+
| Write a Test | <--- (Test Fails - RED)
+--------+---------+
|
v
+------------------+
| Write Code to | <--- (Test Passes - GREEN)
| Pass the Test |
+--------+---------+
|
v
+------------------+
| Refactor Code | <--- (Clean Code - REFACTOR)
+--------+---------+
|
v
(Repeat Cycle)
Advantages of TDD
| # | Advantage | Description |
|---|---|---|
| 1 | Early Bug Detection | Bugs are identified at an early stage since tests are written before code. |
| 2 | Better Code Quality | Forces developers to write clean, modular, and well-structured code. |
| 3 | Simplified Debugging | When a test fails, only the recently written code needs to be checked. |
| 4 | Living Documentation | Test cases serve as documentation that describes how the system should behave. |
| 5 | Confidence in Refactoring | Developers can refactor code confidently knowing tests will catch any regression. |
| 6 | Reduced Cost of Changes | Defects found early are cheaper to fix than those found after deployment. |
| 7 | Better Design | Encourages loosely coupled and highly cohesive design since code must be testable. |
| 8 | Faster Feedback | Developers get immediate feedback on whether their code works correctly. |
| 9 | Regression Testing | The test suite automatically checks that new changes do not break existing functionality. |
| 10 | Increased Developer Confidence | Developers feel more confident releasing software when all tests pass. |
Disadvantages of TDD
| # | Disadvantage | Description |
|---|---|---|
| 1 | Slow Initial Development | Writing tests before code takes extra time initially, slowing down the development speed. |
| 2 | Difficult for Beginners | Requires a shift in mindset; new developers may find it hard to write tests before code. |
| 3 | Maintenance Overhead | As the codebase grows, maintaining a large number of test cases becomes complex and time-consuming. |
| 4 | Not Suitable for All Projects | TDD may not be practical for small, short-lived projects or rapid prototyping. |
| 5 | False Sense of Security | Passing all tests does not guarantee the software is completely bug-free or meets user needs. |
| 6 | Poorly Written Tests | If test cases are poorly designed, they may not effectively catch real bugs. |
| 7 | UI Testing is Difficult | TDD is less effective for testing graphical user interfaces (GUIs) and visual components. |
| 8 | Requires Discipline | TDD requires strict discipline and commitment from the entire development team. |
| 9 | Integration Issues | Unit tests in TDD may pass individually but integration problems may still exist. |
| 10 | Legacy Code Challenges | Applying TDD to existing legacy codebases is very difficult and often impractical. |
Summary
| Aspect | Detail |
|---|---|
| Full Form | Test Driven Development |
| Core Idea | Write tests before writing code |
| Cycle | Red --> Green --> Refactor |
| Goal | Produce clean, working, and well-tested software |
| Best Suited For | Agile and iterative development environments |
Conclusion: TDD is a powerful development methodology that improves code quality, reduces bugs, and increases developer confidence. However, it requires discipline, proper training, and is most effective when applied consistently throughout the development lifecycle.
2081 · 5 marks
Write short notes on: a) open-source development b) Test driven development [5]
Show solved answerHide answer
Short Notes
a) Open-Source Development
Open-source development is a software development model where the source code is made publicly available for anyone to view, use, modify, and distribute. It follows a collaborative, community-driven approach.
Key Characteristics:
- Public Repository: Source code is hosted on platforms like GitHub, GitLab, or SourceForge, accessible to everyone.
- Community Collaboration: Developers worldwide contribute bug fixes, new features, and improvements through pull requests and patches.
- Transparency: All changes, discussions, and decisions are visible to the public.
- Licensing: Governed by licenses such as GNU GPL, MIT, or Apache, which define terms of use and redistribution.
- Peer Review: Code is reviewed by multiple contributors, improving quality and security.
Examples:
Linux, Apache, Mozilla Firefox, Python.
Advantages:
- Reduces development cost
- Faster bug detection and fixing
- Encourages innovation and reuse
Disadvantages:
- Lack of formal support
- Inconsistent documentation
- Security risks if code is misused
b) Test-Driven Development (TDD)
Test-Driven Development (TDD) is a software development practice where tests are written before the actual code. It follows a short, repetitive development cycle.
The TDD Cycle (Red-Green-Refactor):
| Step | Description |
|---|---|
| Red | Write a test that fails (since code does not exist yet) |
| Green | Write the minimum code necessary to pass the test |
| Refactor | Clean and improve the code without changing its behavior |
Key Principles:
- No production code is written unless there is a failing test to justify it.
- Tests act as living documentation of the system.
- Encourages simple, modular, and loosely coupled design.
Process Flow:
Write Failing Test --> Write Code to Pass Test --> Refactor --> Repeat
Advantages:
- Early detection of defects
- Improved code quality and design
- Provides a safety net for future changes (regression testing)
- Better requirement understanding before coding
Disadvantages:
- Requires discipline and practice
- Initial development may be slower
- Difficult to apply for complex UI or legacy systems
Example:
Before writing a function add(a, b), a developer first writes:
assert add(2, 3) == 5 # Test written first
Then implements the function to make the test pass.
2079 · 5 marks
Why test driven development approach is productive in software development? Explain. [5]
Release testing
2082 · 5 marks
Explain the concept of release testing with an example. [5]
Unit testing
2081 · 10 marks
Explain key activities performed during Unit testing, component testing and system testing.[10]
Black box testing
2080 · 5 marks
Differentiate between black box testing and white box testing. [5]
0 · 5 marks
Differentiate between black box testing and white box testing. Explain the importance of white box testing. [5]
Types of testing
0 · 5 marks
List different types of testing. Explain component testing and integration testing. [5]
Make Unit 6 stick
Practice BIT302 with flashcards & quizzes