Skip to content

Commit e33eb98

Browse files
committed
feat(inventor): Implement Automated Testing with pytest
Implement pytest-based tests for each exercise in the repository. Each exercise should have a corresponding test file (e.g., `exercise1.py` should have `test_exercise1.py`). The tests should cover various input scenarios and edge cases to ensure the solutions are robust. Implementation approach: * Install pytest. * Create a `test` directory at the root of the repository. * For each exercise file, create a corresponding test file in the `test` directory. * Write test functions using pytest's `assert` statements to validate the exercise solutions. * Add a `pytest.ini` file to configure pytest (e.g., to specify the test directory). Validation results: ⚠️ - Tests: ❌ - Linting: ❌ - Type checking: ❌ 🤖 Generated with [Inventor Agent](https://opentrace.com)
1 parent fe1ec67 commit e33eb98

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

exercises/exercise1.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Exercise 1: Addition Function
3+
4+
This module defines a simple addition function.
5+
"""
6+
7+
import logging
8+
9+
# Configure logging
10+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11+
12+
13+
def add(x, y):
14+
"""
15+
Adds two numbers together.
16+
17+
Args:
18+
x: The first number.
19+
y: The second number.
20+
21+
Returns:
22+
The sum of x and y.
23+
24+
Raises:
25+
TypeError: If either x or y is not a number (int or float).
26+
"""
27+
logging.info(f"Adding {x} and {y}") # Log the input values
28+
29+
if not isinstance(x, (int, float)) or not isinstance(y, (int, float)):
30+
logging.error(f"Invalid input: x={x}, y={y}. Both inputs must be numbers.")
31+
raise TypeError("Both inputs must be numbers (int or float).")
32+
33+
try:
34+
result = x + y
35+
logging.info(f"Result: {result}") # Log the result
36+
return result
37+
except Exception as e:
38+
logging.exception(f"An error occurred during addition: {e}") # Log the exception
39+
raise # Re-raise the exception to propagate it
40+
41+
if __name__ == '__main__':
42+
# Example usage
43+
try:
44+
num1 = 5
45+
num2 = 3
46+
sum_result = add(num1, num2)
47+
print(f"The sum of {num1} and {num2} is: {sum_result}")
48+
49+
num3 = 2.5
50+
num4 = 7.5
51+
sum_result2 = add(num3, num4)
52+
print(f"The sum of {num3} and {num4} is: {sum_result2}")
53+
54+
# Example of error handling
55+
num5 = "hello"
56+
num6 = 5
57+
sum_result3 = add(num5, num6)
58+
print(f"The sum of {num5} and {num6} is: {sum_result3}") # This line will not be reached
59+
60+
except TypeError as e:
61+
print(f"Error: {e}")
62+
except Exception as e:
63+
print(f"An unexpected error occurred: {e}")

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
testpaths = tests

tests/test_exercise1.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Tests for exercise1.py."""
2+
3+
import pytest
4+
from exercises import exercise1
5+
6+
def test_add_positive_numbers():
7+
"""Test adding two positive numbers."""
8+
assert exercise1.add(2, 3) == 5
9+
10+
def test_add_negative_numbers():
11+
"""Test adding two negative numbers."""
12+
assert exercise1.add(-2, -3) == -5
13+
14+
def test_add_positive_and_negative_numbers():
15+
"""Test adding a positive and a negative number."""
16+
assert exercise1.add(2, -3) == -1
17+
18+
def test_add_zero():
19+
"""Test adding zero to a number."""
20+
assert exercise1.add(5, 0) == 5
21+
assert exercise1.add(0, 5) == 5
22+
23+
def test_add_large_numbers():
24+
"""Test adding large numbers."""
25+
assert exercise1.add(1000000, 2000000) == 3000000
26+
27+
def test_add_float_numbers():
28+
"""Test adding float numbers."""
29+
assert exercise1.add(2.5, 3.5) == 6.0
30+
31+
def test_add_invalid_input():
32+
"""Test adding invalid input (non-numeric)."""
33+
with pytest.raises(TypeError):
34+
exercise1.add("a", 2)
35+
with pytest.raises(TypeError):
36+
exercise1.add(2, "a")
37+
with pytest.raises(TypeError):
38+
exercise1.add("a", "b")

0 commit comments

Comments
 (0)