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:
-
Create a
Calculatorclass: This class should have the following methods:add(a, b): Returns the sum ofaandb.subtract(a, b): Returns the difference ofaandb(a - b).multiply(a, b): Returns the product ofaandb.divide(a, b): Returns the result ofadivided byb. This method should handle division by zero gracefully.
-
Write unit tests using
unittest: Create a separate test class that inherits fromunittest.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
dividemethod needs specific tests for:- Valid division (producing a float or integer).
- Division by zero, ensuring it raises an appropriate exception (e.g.,
ZeroDivisionError).
-
Structure your tests: Organize your tests logically within the test class, using descriptive method names that start with
test_.
Expected Behavior:
- The
Calculatormethods should perform their respective operations correctly for valid inputs. - The
dividemethod should raise aZeroDivisionErrorwhen the divisor is zero. - Your unit tests should pass when executed against a correctly implemented
Calculatorclass.
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(or5) -
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): RaiseZeroDivisionError -
Explanation: Attempting to divide by zero should trigger an exception.
Constraints
- The
Calculatorclass methods should accept numerical inputs (integers or floats). - The
unittestmodule should be used for testing. - Your test file should be a separate Python file from the
Calculatorclass. - All tests should be comprehensive enough to cover the scenarios described.
Notes
- You will need to create two Python files: one for the
Calculatorclass and one for its unit tests. - Remember to import the
unittestmodule and theCalculatorclass 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.