
# Testing Patterns for TinyTorch

## Core Requirements

**ALWAYS USE PYTEST** - TinyTorch uses pytest as the standard testing framework for all tests. Never use manual testing, unittest, or custom test runners.

**ALWAYS USE REAL DATA** - Tests must use actual datasets (CIFAR-10, ImageNet), not mock/synthetic data.

## Test File Structure

### Module Tests (`modules/{module}/tests/test_{module}.py`)
- Test educational module implementations
- Validate both student TODOs and complete implementations
- Import from parent module's development file
- Use pytest classes and assertions

### Package Tests (`tinytorch/tests/`)
- Test exported package functionality
- Integration tests across components
- Production-level validation

## Test Structure Pattern (REQUIRED)

```python
import pytest
import sys
import os

# Add parent directory to path for module imports
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

# Import from the module's development file
from {module}_dev import ComponentName

class TestComponentName:
    """Test suite for ComponentName functionality."""
    
    def test_basic_functionality(self):
        """Test basic component operations."""
        # Arrange
        component = ComponentName()
        
        # Act
        result = component.method()
        
        # Assert
        assert result == expected_value
    
    def test_edge_cases(self):
        """Test edge cases and error conditions."""
        with pytest.raises(ValueError):
            ComponentName(invalid_input)
    
    def test_integration(self):
        """Test integration with other components."""
        # Test how this component works with others
        pass
```

## Real Data Testing Examples

### ✅ GOOD: Actual Datasets
```python
def test_cifar10_dataset():
    """Test with actual CIFAR-10 data."""
    dataset = CIFAR10Dataset('data/cifar10/', train=True, download=True)
    assert len(dataset) == 50000  # Actual CIFAR-10 size
    image, label = dataset[0]
    assert image.shape == (3, 32, 32)  # Real image dimensions
    assert 0 <= label <= 9  # Real class labels
```

### ❌ BAD: Mock/Synthetic Data
```python
# DON'T DO THIS - Violates real data principle
def test_mock_dataset():
    dataset = MockDataset(size=100)  # Fake data
    assert len(dataset) == 100  # Meaningless test
```

## pytest Features to Use

- **Test classes** for organizing related tests
- **pytest fixtures** for setup/teardown
- **Parametrized tests** for testing multiple inputs
- **pytest.raises()** for exception testing
- **assert statements** with descriptive messages

## Running Tests

```bash
# Test specific module (recommended)
python bin/tito.py test --module tensor

# Test all modules
python bin/tito.py test --all

# Run specific test file directly
python -m pytest modules/tensor/tests/test_tensor.py -v
```

## Key Principles

- **Always use pytest** - No exceptions, no manual testing
- **Always use real data** - No mocks, no synthetic data
- Tests should pass with both TODO stubs and complete implementations
- Educational tests should guide student learning with real examples
- Package tests ensure production readiness
- Use descriptive test names that explain what's being tested
- Import from module development files, not the package (for module tests)
- Group related tests in test classes
 

This ensures all tests run through pytest with consistent output and reporting, using real data throughout the testing process.
 