Skip to content

Commit 138e260

Browse files
committed
Day 28 - Testing
1 parent c2fab77 commit 138e260

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

Day 28 - Testing/Commands.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```bash
2+
# Run all tests
3+
pytest -v
4+
5+
# Run with coverage
6+
pytest --cov=myapp --cov-report=html
7+
8+
# Run specific tests
9+
pytest -k "test_multiply"
10+
```

Day 28 - Testing/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Day 28: Testing
2+
3+
## What You'll Learn:
4+
- Writing test cases with `unittest`
5+
- pytest framework features
6+
- Test fixtures and parametrization
7+
- Mocking external dependencies
8+
- Measuring test coverage
9+
10+
## Files:
11+
1. `test_math_utils.py` - Basic test cases
12+
2. `test_fixtures.py` - pytest fixtures
13+
3. `parametrized_tests.py` - Data-driven testing
14+
4. `test_coverage.py` - Coverage analysis
15+
16+
## Exercises:
17+
1. Create test suite for a calculator module that:
18+
- Tests all arithmetic operations
19+
- Covers edge cases (division by zero)
20+
- Mocks file I/O operations
21+
22+
2. Build test coverage for an API client:
23+
- Mock HTTP responses
24+
- Test error handling
25+
- Validate JSON parsing
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
from calculator import multiply
3+
4+
@pytest.mark.parametrize("a,b,expected", [
5+
(2, 3, 6),
6+
(-4, 5, -20),
7+
(0, 100, 0),
8+
])
9+
def test_multiply(a, b, expected):
10+
assert multiply(a, b) == expected

Day 28 - Testing/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest==7.4.0
2+
pytest-cov==4.1.0

Day 28 - Testing/test_coverage.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# File to test coverage for
2+
def calculate_stats(numbers):
3+
return {
4+
"mean": sum(numbers)/len(numbers),
5+
"min": min(numbers),
6+
"max": max(numbers)
7+
}
8+
9+
# Test file
10+
def test_stats():
11+
result = calculate_stats([10, 20, 30])
12+
assert result["mean"] == 20
13+
assert result["min"] == 10
14+
assert result["max"] == 30

Day 28 - Testing/test_fixtures.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
@pytest.fixture
4+
def database():
5+
"""Fixture providing test database connection"""
6+
db = connect_to_test_db()
7+
yield db
8+
db.disconnect()
9+
10+
def test_user_count(database):
11+
assert database.get_user_count() == 0
12+
13+
@pytest.fixture
14+
def sample_user():
15+
return {"name": "Alice", "email": "alice@example.com"}
16+
17+
def test_user_creation(sample_user):
18+
assert "@" in sample_user["email"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from math_utils import add, subtract, divide
3+
4+
class TestMathUtils(unittest.TestCase):
5+
def test_add(self):
6+
self.assertEqual(add(2, 3), 5)
7+
self.assertEqual(add(-1, 1), 0)
8+
9+
def test_subtract(self):
10+
self.assertEqual(subtract(5, 2), 3)
11+
12+
def test_divide(self):
13+
self.assertAlmostEqual(divide(10, 3), 3.333, places=2)
14+
with self.assertRaises(ValueError):
15+
divide(5, 0)
16+
17+
if __name__ == '__main__':
18+
unittest.main()

0 commit comments

Comments
 (0)