0
# Testing Utilities
1
2
Indico provides comprehensive testing infrastructure including pytest integration, extensive fixture systems, and utilities for creating parameterized tests. The testing framework supports both core development and plugin testing with realistic data and proper application context.
3
4
## Capabilities
5
6
### Pytest Integration
7
8
Indico includes a pytest plugin that automatically configures the testing environment and provides access to all testing fixtures.
9
10
```python { .api }
11
# Pytest plugin is automatically registered via entry point
12
# No explicit import needed - fixtures are available when using pytest
13
```
14
15
Configuration in `pyproject.toml`:
16
```toml
17
[project.entry-points."pytest11"]
18
indico = "indico.testing.pytest_plugin"
19
```
20
21
### Test Utilities
22
23
Core testing utility functions for creating parameterized tests and handling test data.
24
25
```python { .api }
26
def bool_matrix(template, mask, expect):
27
"""
28
Create boolean matrix for parameterized tests.
29
30
Generates test parameter combinations based on a template with boolean
31
placeholders, applying a mask to filter combinations, and mapping results
32
to expected outcomes.
33
34
Parameters:
35
- template: str or callable, test parameter template
36
- mask: dict, mask to apply to boolean combinations
37
- expect: dict, expected results for each combination
38
39
Returns:
40
list: List of pytest parameter tuples for @pytest.mark.parametrize
41
"""
42
```
43
44
Usage example:
45
46
```python
47
import pytest
48
from indico.testing.util import bool_matrix
49
50
# Create parameterized test cases
51
test_cases = bool_matrix(
52
template="user={user_exists}, admin={is_admin}",
53
mask={'user_exists': True, 'is_admin': [True, False]},
54
expect={
55
(True, True): 'admin_access',
56
(True, False): 'user_access'
57
}
58
)
59
60
@pytest.mark.parametrize("params,expected", test_cases)
61
def test_access_control(params, expected):
62
# Test implementation
63
pass
64
```
65
66
### Core Fixtures
67
68
Essential fixtures for application testing and database operations.
69
70
#### Application Context Fixtures
71
72
```python
73
# Available automatically when using Indico's pytest plugin
74
75
def app():
76
"""Flask application instance with test configuration."""
77
78
def request_context():
79
"""Flask request context for testing."""
80
81
def db():
82
"""Database session for testing."""
83
```
84
85
#### User and Authentication Fixtures
86
87
```python
88
def dummy_user():
89
"""Create a test user instance."""
90
91
def create_user():
92
"""Factory function for creating test users."""
93
94
def dummy_admin():
95
"""Create a test admin user."""
96
```
97
98
#### Event and Content Fixtures
99
100
```python
101
def dummy_event():
102
"""Create a test event instance."""
103
104
def create_event():
105
"""Factory function for creating test events."""
106
107
def dummy_category():
108
"""Create a test category."""
109
110
def create_category():
111
"""Factory function for creating test categories."""
112
```
113
114
#### Contribution and Session Fixtures
115
116
```python
117
def dummy_contribution():
118
"""Create a test contribution."""
119
120
def create_contribution():
121
"""Factory function for creating test contributions."""
122
123
def dummy_session():
124
"""Create a test session."""
125
126
def create_session():
127
"""Factory function for creating test sessions."""
128
```
129
130
#### Room Booking Fixtures
131
132
```python
133
def dummy_room():
134
"""Create a test room."""
135
136
def create_room():
137
"""Factory function for creating test rooms."""
138
139
def dummy_reservation():
140
"""Create a test room reservation."""
141
142
def create_reservation():
143
"""Factory function for creating reservations."""
144
```
145
146
#### Abstract and Submission Fixtures
147
148
```python
149
def dummy_abstract():
150
"""Create a test abstract submission."""
151
152
def create_abstract():
153
"""Factory function for creating test abstracts."""
154
```
155
156
### Cache Fixtures
157
158
Fixtures for testing caching functionality and cache-dependent features.
159
160
```python
161
def redis_proc():
162
"""Redis process fixture for cache testing."""
163
164
def redis():
165
"""Redis client fixture."""
166
```
167
168
### Testing Best Practices
169
170
#### Using Fixtures in Tests
171
172
```python
173
def test_user_creation(create_user, db):
174
"""Test user creation with fixtures."""
175
user = create_user(email='test@example.com', first_name='Test')
176
db.session.add(user)
177
db.session.commit()
178
179
assert user.email == 'test@example.com'
180
assert user.first_name == 'Test'
181
182
def test_event_management(dummy_event, dummy_user):
183
"""Test event management functionality."""
184
event = dummy_event
185
user = dummy_user
186
187
# Test event operations
188
assert event.title
189
assert event.creator == user
190
```
191
192
#### Parameterized Testing
193
194
```python
195
@pytest.mark.parametrize("user_type,access_level", [
196
('admin', 'full'),
197
('manager', 'limited'),
198
('user', 'read_only')
199
])
200
def test_access_levels(user_type, access_level, create_user):
201
"""Test different user access levels."""
202
user = create_user(user_type=user_type)
203
# Test access level logic
204
pass
205
```
206
207
#### Plugin Testing
208
209
```python
210
def test_plugin_functionality(app, dummy_event):
211
"""Test plugin functionality."""
212
from my_plugin import MyPlugin
213
214
plugin = MyPlugin(app.extensions['indico_plugins'])
215
plugin.init()
216
217
# Test plugin behavior
218
result = plugin.process_event(dummy_event)
219
assert result is not None
220
```
221
222
### Email Testing Utilities
223
224
Utilities for testing email functionality in events and notifications.
225
226
```python
227
# Email testing utilities are available in indico.testing.util
228
# Specific functions for email validation and testing
229
```
230
231
### Data Processing Helpers
232
233
YAML and JSON processing helpers for test data management.
234
235
```python
236
# YAML and JSON processing utilities available in indico.testing.util
237
# Functions for loading and processing test data files
238
```
239
240
## Test Configuration
241
242
The pytest plugin automatically configures:
243
244
- Test database setup and teardown
245
- Application context management
246
- Fixture registration and dependency injection
247
- Test-specific configuration overrides
248
- Cleanup after test execution
249
250
## Running Tests
251
252
```bash
253
# Run all tests
254
pytest
255
256
# Run specific test file
257
pytest tests/core/test_plugins.py
258
259
# Run tests with coverage
260
pytest --cov=indico
261
262
# Run tests for specific plugin
263
pytest tests/plugins/test_my_plugin.py
264
```