Hone logo
Hone
Problems

Unit Testing a Simple Calculator Class

This challenge focuses on developing robust unit tests for a basic Python class that performs arithmetic operations. Writing effective unit tests is crucial for ensuring code correctness, preventing regressions, and facilitating future development. You will use Python's built-in unittest module to achieve this.

Problem Description

Your task is to create a suite of unit tests for a Calculator class. This class will have methods for addition, subtraction, multiplication, and division. The tests should cover various scenarios, including positive and negative numbers, zero, and division by zero.

Key Requirements:

  1. Create a Calculator class: This class should have the following methods:

    • add(a, b): Returns the sum of a and b.
    • subtract(a, b): Returns the difference of a and b (a - b).
    • multiply(a, b): Returns the product of a and b.
    • divide(a, b): Returns the result of a divided by b. This method should handle division by zero gracefully.
  2. Write unit tests using unittest: Create a separate test class that inherits from unittest.TestCase.

    • Each arithmetic operation should have at least three test cases.
    • Test cases should cover:
      • Positive integers.
      • Negative integers.
      • A mix of positive and negative integers.
      • Operations involving zero.
    • The divide method needs specific tests for:
      • Valid division (producing a float or integer).
      • Division by zero, ensuring it raises an appropriate exception (e.g., ZeroDivisionError).
  3. Structure your tests: Organize your tests logically within the test class, using descriptive method names that start with test_.

Expected Behavior:

  • The Calculator methods should perform their respective operations correctly for valid inputs.
  • The divide method should raise a ZeroDivisionError when the divisor is zero.
  • Your unit tests should pass when executed against a correctly implemented Calculator class.

Examples

Example 1: Addition Tests

  • Input to Calculator.add: a = 5, b = 3

  • Expected Output of add(5, 3): 8

  • Explanation: Standard addition of two positive integers.

  • Input to Calculator.add: a = -2, b = -7

  • Expected Output of add(-2, -7): -9

  • Explanation: Addition involving two negative integers.

  • Input to Calculator.add: a = 10, b = 0

  • Expected Output of add(10, 0): 10

  • Explanation: Addition involving zero.

Example 2: Division Tests

  • Input to Calculator.divide: a = 10, b = 2

  • Expected Output of divide(10, 2): 5.0 (or 5)

  • Explanation: Standard division of two positive integers.

  • Input to Calculator.divide: a = -15, b = 3

  • Expected Output of divide(-15, 3): -5.0 (or -5)

  • Explanation: Division involving a negative dividend.

  • Input to Calculator.divide: a = 7, b = 0

  • Expected Behavior of divide(7, 0): Raise ZeroDivisionError

  • Explanation: Attempting to divide by zero should trigger an exception.

Constraints

  • The Calculator class methods should accept numerical inputs (integers or floats).
  • The unittest module should be used for testing.
  • Your test file should be a separate Python file from the Calculator class.
  • All tests should be comprehensive enough to cover the scenarios described.

Notes

  • You will need to create two Python files: one for the Calculator class and one for its unit tests.
  • Remember to import the unittest module and the Calculator class into your test file.
  • For testing exceptions, use self.assertRaises() within your test methods.
  • Consider floating-point precision for division results if necessary, though for this challenge, direct equality comparison for simple cases is likely sufficient.
Loading editor...
python