Spaces:
Running
Running
Fetching metadata from the HF Docker repository...
Unit Tests for PromptAid Vision Backend
This directory contains pure unit tests for the PromptAid Vision backend application. These tests are designed to be fast, isolated, and run without external dependencies.
π§ͺ What Are Unit Tests?
Unit tests are tests that:
- β Test individual functions/methods in isolation
- β Mock external dependencies (databases, APIs, file systems)
- β Run quickly (milliseconds, not seconds)
- β Don't require running services
- β Don't make network calls
- β Don't require database connections
π Test Organization
Services Layer
test_schema_validator.py
- Schema validation logic teststest_image_preprocessor.py
- Image processing and validation teststest_vlm_service.py
- VLM service logic tests (mocked APIs)
Basic Tests
test_basic.py
- Basic testing infrastructure verification
π Running Unit Tests
Run All Unit Tests
cd py_backend/tests/unit_tests
python run_unit_tests.py
Run Individual Test Files
cd py_backend/tests/unit_tests
python -m unittest test_schema_validator.py
python -m unittest test_image_preprocessor.py
python -m unittest test_vlm_service.py
python -m unittest test_basic.py
Run Specific Test Classes
cd py_backend/tests/unit_tests
python -m unittest test_schema_validator.TestSchemaValidator
python -m unittest test_image_preprocessor.TestImagePreprocessor
python -m unittest test_vlm_service.TestVLMServiceManager
Run Specific Test Methods
cd py_backend/tests/unit_tests
python -m unittest test_schema_validator.TestSchemaValidator.test_validate_crisis_map_data_valid
π§ Test Structure
Each test file follows this pattern:
class TestComponentName(unittest.TestCase):
def setUp(self):
"""Set up test fixtures"""
pass
def test_method_name_scenario(self):
"""Test description"""
# Arrange - Set up test data
# Act - Execute the method being tested
# Assert - Verify the results
pass
π Test Coverage
Schema Validator (15+ tests)
- Crisis map data validation
- Drone data validation
- Data cleaning and transformation
- Error handling for invalid data
- Schema validation against JSON schemas
Image Preprocessor (10+ tests)
- MIME type detection
- Preprocessing requirements checking
- Image format conversion
- Configuration constants
- PyMuPDF availability
VLM Service (15+ tests)
- Service registration and management
- Model type enumeration
- Stub service functionality
- Service availability checking
Basic Tests (8 tests)
- Python environment verification
- Testing infrastructure validation
- Basic operations testing
π― Best Practices
Test Naming
- Use descriptive test names:
test_validate_crisis_map_data_missing_analysis
- Follow pattern:
test_methodName_scenario
Test Organization
- Group related tests in test classes
- Use
setUp()
for common test data - Use
tearDown()
for cleanup if needed
Assertions
- Use specific assertions:
assertEqual()
,assertIn()
,assertTrue()
- Avoid generic
assert
statements - Test both positive and negative cases
Mocking
- Mock external dependencies
- Mock database connections
- Mock API calls
- Mock file system operations
π¨ What NOT to Test
- β Database connections (use mocks)
- β External API calls (use mocks)
- β File system operations (use mocks)
- β Network requests
- β Slow operations
π Debugging Tests
Verbose Output
python -m unittest -v test_schema_validator.py
Stop on First Failure
python -m unittest -f test_schema_validator.py
Run with Coverage
pip install coverage
coverage run -m unittest discover
coverage report
coverage html # Generate HTML report
π Adding New Tests
- Create test file:
test_new_component.py
- Import the component:
from app.components.new_component import NewComponent
- Create test class:
class TestNewComponent(unittest.TestCase):
- Write test methods: Follow the Arrange-Act-Assert pattern
- Mock dependencies: Use
unittest.mock
for external dependencies - Run tests: Ensure they pass before committing
π Benefits of Unit Tests
- Fast Feedback: Tests run in milliseconds
- Isolated Testing: No external dependencies
- Easy Debugging: Clear failure points
- Confidence: Know your code works in isolation
- Documentation: Tests show how to use your code
- Refactoring Safety: Catch regressions quickly