Hone logo
Hone
Problems

Testing a Simple Calculator with unittest

This challenge focuses on writing unit tests in Python using the unittest framework. You'll be testing a basic calculator class, ensuring its methods (addition, subtraction, multiplication, and division) function correctly under various conditions, including edge cases like division by zero. This exercise reinforces understanding of test-driven development and robust code validation.

Problem Description

You are provided with a Calculator class. Your task is to create a suite of unittest tests to thoroughly validate the functionality of this class. The tests should cover:

  • Basic Arithmetic: Verify that addition, subtraction, multiplication, and division produce the correct results for positive and negative numbers.
  • Zero Input: Test the behavior of each operation when one or both inputs are zero.
  • Division by Zero: Specifically, ensure that the divide method raises a ZeroDivisionError when attempting to divide by zero. The test should assert that this exception is raised.
  • Floating-Point Numbers: Test with floating-point numbers to ensure accuracy (within reasonable tolerances, as floating-point arithmetic can be imprecise).
  • Negative Numbers: Test with negative numbers to ensure correct handling of signs.

The goal is to create a comprehensive test suite that provides high confidence in the Calculator class's correctness.

Examples

Example 1:

Input: Calculator(5, 3)
Output:
    add() returns 8
    subtract() returns 2
    multiply() returns 15
    divide() returns 1.6666666666666667
Explanation:  The Calculator object is initialized with 5 and 3. The tests should verify the results of each operation.

Example 2:

Input: Calculator(10, 0)
Output:
    add() returns 10
    subtract() returns 10
    multiply() returns 0
    divide() raises ZeroDivisionError
Explanation:  Testing with zero as a divisor should result in a ZeroDivisionError. The test should assert this.

Example 3: (Edge Case)

Input: Calculator(-2, 4)
Output:
    add() returns 2
    subtract() returns -6
    multiply() returns -8
    divide() returns -0.5
Explanation: Testing with negative numbers to ensure correct sign handling.

Constraints

  • You must use the unittest framework in Python.
  • The Calculator class is provided below. Do not modify the Calculator class.
  • Your tests should be well-structured and easy to understand.
  • All test cases must pass without errors.
  • The tests should cover all the scenarios described in the Problem Description.

Notes

  • Consider using self.assertEqual(), self.assertTrue(), self.assertFalse(), and self.assertRaises() methods from the unittest framework.
  • Think about how to structure your test class and test methods for clarity and maintainability.
  • Remember to import the unittest module and the Calculator class.
  • The Calculator class is defined as follows:
class Calculator:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def add(self):
        return self.x + self.y

    def subtract(self):
        return self.x - self.y

    def multiply(self):
        return self.x * self.y

    def divide(self):
        if self.y == 0:
            raise ZeroDivisionError("Cannot divide by zero.")
        return self.x / self.y
Loading editor...
python