added tests
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""Tests for the calculator module."""
|
||||
|
||||
import pytest
|
||||
from test_package.calculator import Calculator
|
||||
|
||||
|
||||
class TestCalculator:
|
||||
"""Test cases for Calculator class."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.calc = Calculator()
|
||||
|
||||
def test_add(self):
|
||||
"""Test addition operation."""
|
||||
assert self.calc.add(2, 3) == 5
|
||||
assert self.calc.add(-1, 1) == 0
|
||||
assert self.calc.add(0.1, 0.2) == pytest.approx(0.3)
|
||||
|
||||
def test_subtract(self):
|
||||
"""Test subtraction operation."""
|
||||
assert self.calc.subtract(5, 3) == 2
|
||||
assert self.calc.subtract(1, 1) == 0
|
||||
assert self.calc.subtract(-1, -2) == 1
|
||||
|
||||
def test_multiply(self):
|
||||
"""Test multiplication operation."""
|
||||
assert self.calc.multiply(3, 4) == 12
|
||||
assert self.calc.multiply(-2, 3) == -6
|
||||
assert self.calc.multiply(0, 100) == 0
|
||||
|
||||
def test_divide(self):
|
||||
"""Test division operation."""
|
||||
assert self.calc.divide(10, 2) == 5.0
|
||||
assert self.calc.divide(7, 2) == 3.5
|
||||
assert self.calc.divide(-6, 3) == -2.0
|
||||
|
||||
def test_divide_by_zero(self):
|
||||
"""Test division by zero raises error."""
|
||||
with pytest.raises(ValueError, match="Cannot divide by zero"):
|
||||
self.calc.divide(5, 0)
|
||||
|
||||
def test_power(self):
|
||||
"""Test power operation."""
|
||||
assert self.calc.power(2, 3) == 8
|
||||
assert self.calc.power(5, 0) == 1
|
||||
assert self.calc.power(2, -1) == 0.5
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Tests for the utils module."""
|
||||
|
||||
import pytest
|
||||
from test_package.utils import format_number, is_even, factorial
|
||||
|
||||
|
||||
class TestUtils:
|
||||
"""Test cases for utility functions."""
|
||||
|
||||
def test_format_number_integer(self):
|
||||
"""Test formatting integers."""
|
||||
assert format_number(42) == "42"
|
||||
assert format_number(-5) == "-5"
|
||||
|
||||
def test_format_number_float(self):
|
||||
"""Test formatting floats."""
|
||||
assert format_number(3.14159) == "3.14"
|
||||
assert format_number(3.14159, 3) == "3.142"
|
||||
assert format_number(2.0) == "2.00"
|
||||
|
||||
def test_is_even(self):
|
||||
"""Test even number detection."""
|
||||
assert is_even(2) is True
|
||||
assert is_even(4) is True
|
||||
assert is_even(1) is False
|
||||
assert is_even(3) is False
|
||||
assert is_even(0) is True
|
||||
assert is_even(-2) is True
|
||||
assert is_even(-1) is False
|
||||
|
||||
def test_factorial_positive(self):
|
||||
"""Test factorial of positive numbers."""
|
||||
assert factorial(0) == 1
|
||||
assert factorial(1) == 1
|
||||
assert factorial(5) == 120
|
||||
assert factorial(3) == 6
|
||||
|
||||
def test_factorial_negative(self):
|
||||
"""Test factorial of negative numbers raises error."""
|
||||
with pytest.raises(ValueError, match="Factorial is not defined for negative numbers"):
|
||||
factorial(-1)
|
||||
Reference in New Issue
Block a user