Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions python/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
"""
Test runner for the Supermarket Receipt Kata unit tests.

This script runs all unit tests and provides coverage information.
"""

import subprocess
import sys

def run_tests():
"""Run all unit tests"""
print("Running unit tests...")
result = subprocess.run([
sys.executable, "-m", "pytest",
"tests/test_supermarket.py",
"-v"
], cwd=".")

if result.returncode != 0:
print("❌ Tests failed!")
return False

print("✅ All tests passed!")
return True

def run_coverage():
"""Run tests with coverage report"""
print("\nRunning tests with coverage...")

# Run tests with coverage
subprocess.run([
sys.executable, "-m", "coverage", "run",
"-m", "pytest", "tests/test_supermarket.py"
], cwd=".")

# Generate coverage report
subprocess.run([
sys.executable, "-m", "coverage", "report",
"catalog.py", "model_objects.py", "receipt.py",
"shopping_cart.py", "teller.py", "receipt_printer.py"
], cwd=".")

if __name__ == "__main__":
if run_tests():
run_coverage()
else:
sys.exit(1)
93 changes: 93 additions & 0 deletions python/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Unit Tests for Supermarket Receipt Kata

This directory contains comprehensive unit tests for the Supermarket Receipt Kata Python implementation.

## Test Coverage

The test suite provides **99% code coverage** across all modules:

- `catalog.py`: 60% (intentional - abstract base class)
- `model_objects.py`: 100%
- `receipt.py`: 100%
- `receipt_printer.py`: 100%
- `shopping_cart.py`: 100%
- `teller.py`: 100%

## Running Tests

### Quick Test Run
```bash
python -m pytest tests/test_supermarket.py -v
```

### With Coverage Report
```bash
python run_tests.py
```

### Manual Coverage Check
```bash
python -m coverage run -m pytest tests/test_supermarket.py
python -m coverage report catalog.py model_objects.py receipt.py shopping_cart.py teller.py receipt_printer.py
```

## Test Scenarios Covered

### Discount Types
- **THREE_FOR_TWO**: Buy 3, pay for 2
- **TEN_PERCENT_DISCOUNT**: Percentage-based discounts
- **TWO_FOR_AMOUNT**: Buy 2 for a fixed price
- **FIVE_FOR_AMOUNT**: Buy 5 for a fixed price

### Test Categories

#### Basic Functionality
- Shopping without discounts
- Empty shopping cart
- Product units (EACH vs KILO)
- Adding same product multiple times

#### Discount Scenarios
- Exact quantities for discounts
- Quantities with remainders
- Insufficient quantities (no discount)
- Multiple discounts on different products
- Large quantities (100+ items)
- Fractional quantities with discounts

#### Receipt Printing
- Basic receipt formatting
- Receipts with discounts
- Different product units
- Price formatting
- Multiple items with different widths

#### Edge Cases
- Zero-price products
- Large quantities
- Mixed product types
- Complex discount combinations

## Test Quality

- **25 comprehensive test cases**
- Clear, descriptive test names
- Detailed assertions checking totals, items, and discounts
- Good separation of arrange/act/assert
- Edge case coverage
- Documentation of expected behavior

## Notes

The tests document current behavior including a potential bug in the `TWO_FOR_AMOUNT` discount calculation. This provides a baseline for future refactoring while maintaining existing functionality.

## Dependencies

- pytest
- pytest-approx (for floating-point comparisons)
- coverage (for coverage reports)

Install with:
```bash
pip install -r requirements.txt
```
Loading