# UI Testing
## Visual Testing Page
Visit: [http://127.0.0.1:8411/testing/ui](http://127.0.0.1:8411/testing/ui)
These test/ui views are only available in development when `DEBUG=True`. They are conditionally loaded in the root `urls.py`.
## UI Testing Framework Guidelines
The visual testing framework is designed for viewing UI styling and layout during development. These are **read-only** views that should never modify system state.
### Critical Principle: System State Isolation
**NEVER modify real system state in UI test views:**
- Do not add data to real managers (AlertManager, WeatherManager, etc.)
- Do not modify database records
- Do not modify in-memory caches or singletons
- Do not persist test data that appears in production views
### Correct Approach: Render Templates Directly
```python
class TestUiAlertDetailsView(View):
def get(self, request, *args, **kwargs):
# Create synthetic data
alert = AlertSyntheticData.create_single_alarm_alert(
alarm_level=AlarmLevel.WARNING,
has_image=True
)
# Prepare context data using domain object methods
visual_content = alert.get_first_visual_content()
# Render template directly with synthetic data
context = {
'alert': alert,
'alert_visual_content': visual_content,
}
return render(request, 'alert/modals/alert_details.html', context)
```
### Architecture Patterns
**Render Templates Directly (Preferred):**
- For testing UI styling and layout
- When you need specific synthetic data scenarios
- When testing requires system state isolation
- Follows pattern used by weather, notify modules
**Code Duplication Prevention:**
- Move shared logic to domain object methods (e.g., `Alert.get_first_visual_content()`)
- Use centralized synthetic data classes
- Create utility functions for common data preparation patterns
## Setting Up Visual Testing
### Auto-Discovery Structure
The `hi.tests.ui` module uses auto-discovery by looking in app directories.
In the app directory you want to have a visual testing page:
```bash
mkdir -p tests/ui
touch tests/__init__.py
touch tests/ui/__init__.py
```
### Required Files
Create these files:
- `tests/ui/views.py`
- `tests/ui/urls.py` (gets auto-discovered)
### Template Structure
Templates go in `templates/${APPNAME}/testing/ui/`. Create a home page:
```html
{% extends "pages/base.html" %}
{% load icons %}
{% block head_title %}HI: {{ app_name|title }} UI Tests{% endblock %}
{% block content %}