Writing simple test cases

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-29

Problem Statement

Design a simple test framework or suite that validates the functionality of a given module or class by writing structured unit test cases.


The goal is to ensure correctness, handle edge cases, and demonstrate test-driven design thinking.


📋 Problem Description


You are required to write test cases for a small, well-defined module (for example: a Calculator class, Linked List implementation, or String Utility).



Your task is to identify test scenarios, organize them into a test structure, and simulate a minimal test execution environment — without using any external testing libraries (like JUnit,

pytest, etc.).


The exercise focuses on your ability to:


•Think critically about edge cases,
•Design reusable test structures, and
•Validate system correctness systematically.


🧱 Core Requirements


1. Classes to Design


•TestCase: Represents a single test case (contains input, expected output, and logic to run).
•TestSuite: Manages multiple test cases, runs them, and reports results.
•TargetModule: The class or module under test (e.g., Calculator).


2. Functional Requirements


You need to:


1.Identify all testable methods in the target class.
2.For each method, write positive, negative, and boundary test cases.
3.Implement a basic test runner to:
•Execute each test case.
•Compare expected vs actual output.
•Print a summary report (passed/failed counts).


🧩 Example Use Case


Module under test:

1 2class Calculator: 3 def add(self, a, b): return a + b 4 def divide(self, a, b): return a / b


Test cases:

1.Test addition of two positive numbers.
2.Test addition with zero.
3.Test division by nonzero number.
4.Test division by zero (expect exception).


Expected Output:

1 2Test Results: 3✅ add_positive_numbers passed 4✅ add_with_zero passed 5✅ divide_normal_case passed 6❌ divide_by_zero failed (Exception: ZeroDivisionError) 7Summary: 3 passed, 1 failed


⚙️ Non-Functional Requirements


•Tests must be independent and repeatable.
•Output should be clear and structured (pass/fail summary).
•Should handle unexpected exceptions gracefully.
•Modular design to allow plugging in other target modules later.


🧠 Design Discussion Points


•How can you make your test framework extensible for future modules?
•Should test cases stop on failure or continue running all?
•How can you measure code coverage or branch coverage conceptually?
•How would you adapt this design to integrate with real test frameworks later?


✅ Deliverables


•Design document (UML for TestCase, TestSuite, and TargetModule).
•Implementation code (e.g., in Python, Java, or C++).
•Console-based test runner output.


Solution

Comments