Skip to content

Commit 7a1838e

Browse files
Add coverage
0 parents  commit 7a1838e

File tree

8 files changed

+293
-0
lines changed

8 files changed

+293
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
__pycache__
2+
*.pytest_cache
3+
venv/
4+
.venv
5+
# .env
6+
*.db
7+
*.idea
8+
*dist
9+
*whl
10+
*htmlcov
11+
.coverage

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

poetry.lock

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[tool.poetry]
2+
name = "pytest-poetry-test-coverage-example"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Your Name <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.12"
10+
11+
12+
[tool.poetry.group.dev.dependencies]
13+
pytest = "^8.3.3"
14+
coverage = "^7.6.1"
15+
16+
[tool.coverage.run]
17+
dynamic_context = "test_function"
18+
19+
[build-system]
20+
requires = ["poetry-core"]
21+
build-backend = "poetry.core.masonry.api"

src/math_operations.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class MathOperations:
2+
3+
def add(self, a, b):
4+
return a + b
5+
6+
def subtract(self, a, b):
7+
return a - b
8+
9+
def multiply(self, a, b):
10+
return a * b
11+
12+
def divide(self, a, b):
13+
if b == 0: # pragma: no cover
14+
raise ValueError("Cannot divide by zero.")
15+
return a / b
16+
17+
def power(self, a, b):
18+
return a**b
19+
20+
def square_root(self, a):
21+
if a < 0:
22+
raise ValueError("Cannot calculate square root of negative number.")
23+
return a**0.5
24+
25+
def factorial(self, a):
26+
if a < 0:
27+
raise ValueError("Cannot calculate factorial of negative number.")
28+
if a == 0:
29+
return 1
30+
return a * self.factorial(a - 1)

src/string_operations.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class StringOperations:
2+
3+
def capitalize(self, text: str) -> str:
4+
return text.capitalize()
5+
6+
def title(self, text: str) -> str:
7+
return text.title()
8+
9+
def upper(self, text: str) -> str:
10+
return text.upper()
11+
12+
def lower(self, text: str) -> str:
13+
return text.lower()
14+
15+
def swapcase(self, text: str) -> str:
16+
return text.swapcase()
17+
18+
def reverse(self, text: str) -> str:
19+
return text[::-1]
20+
21+
def strip(self, text: str) -> str:
22+
return text.strip()

tests/__init__.py

Whitespace-only changes.

tests/test_math_operations.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
from src.math_operations import MathOperations
3+
4+
5+
@pytest.fixture
6+
def math_ops():
7+
return MathOperations()
8+
9+
10+
def test_add(math_ops):
11+
assert math_ops.add(10, 5) == 15
12+
assert math_ops.add(-1, 1) == 0
13+
14+
15+
def test_subtract(math_ops):
16+
assert math_ops.subtract(10, 5) == 5
17+
assert math_ops.subtract(5, 10) == -5
18+
19+
20+
def test_multiply(math_ops):
21+
assert math_ops.multiply(3, 4) == 12
22+
assert math_ops.multiply(0, 5) == 0
23+
24+
25+
def test_divide(math_ops):
26+
assert math_ops.divide(10, 2) == 5
27+
# with pytest.raises(ValueError, match="Cannot divide by zero."):
28+
# math_ops.divide(10, 0)
29+
30+
31+
def test_power(math_ops):
32+
assert math_ops.power(2, 3) == 8
33+
assert math_ops.power(5, 0) == 1
34+
35+
36+
def test_square_root(math_ops):
37+
assert math_ops.square_root(9) == 3
38+
with pytest.raises(
39+
ValueError, match="Cannot calculate square root of negative number."
40+
):
41+
math_ops.square_root(-1)
42+
43+
44+
def test_factorial(math_ops):
45+
assert math_ops.factorial(5) == 120
46+
assert math_ops.factorial(0) == 1
47+
with pytest.raises(
48+
ValueError, match="Cannot calculate factorial of negative number."
49+
):
50+
math_ops.factorial(-1)

0 commit comments

Comments
 (0)