0
# Configuration
1
2
Flask application configuration access and manipulation during tests. Provides direct access to app configuration and runtime configuration changes through pytest markers.
3
4
## Capabilities
5
6
### Configuration Access
7
8
Direct access to the Flask application's configuration dictionary for reading and asserting configuration values during tests.
9
10
```python { .api }
11
@pytest.fixture
12
def config(app):
13
"""
14
An application config.
15
16
Parameters:
17
app: Flask application instance (from app fixture)
18
19
Returns:
20
Config: Flask application config object (werkzeug.datastructures.ImmutableDict)
21
"""
22
```
23
24
**Usage Example:**
25
26
```python
27
def test_config_values(config):
28
assert config['TESTING'] is True
29
assert config['SECRET_KEY'] is not None
30
assert config['DATABASE_URL'] == 'sqlite:///:memory:'
31
32
def test_debug_mode(app, config):
33
# Access config through fixture
34
assert config['DEBUG'] == app.debug
35
36
def test_environment_config(config):
37
# Check environment-specific settings
38
if config.get('ENV') == 'testing':
39
assert config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite')
40
else:
41
assert config['SQLALCHEMY_DATABASE_URI'].startswith('postgresql')
42
```
43
44
### Options Marker
45
46
Configure Flask application settings for specific tests using the `@pytest.mark.options` decorator. Options are applied to the application config before the test runs.
47
48
```python { .api }
49
@pytest.mark.options(**kwargs)
50
def test_function():
51
"""
52
Decorator to pass options to your application factory.
53
54
Parameters:
55
**kwargs: Configuration key-value pairs to set on app.config
56
57
Usage:
58
@pytest.mark.options(debug=False, testing=True)
59
def test_something(app):
60
assert not app.debug
61
assert app.config['TESTING']
62
"""
63
```
64
65
**Usage Examples:**
66
67
```python
68
@pytest.mark.options(debug=False)
69
def test_production_mode(app, config):
70
"""Test with debug mode disabled"""
71
assert not app.debug
72
assert config['DEBUG'] is False
73
74
@pytest.mark.options(testing=True, debug=True, secret_key='test-secret')
75
def test_with_multiple_options(app, config):
76
"""Test with multiple configuration options"""
77
assert app.debug is True
78
assert config['TESTING'] is True
79
assert config['SECRET_KEY'] == 'test-secret'
80
81
@pytest.mark.options(
82
database_url='postgresql://test:test@localhost/testdb',
83
redis_url='redis://localhost:6379/1'
84
)
85
def test_external_services_config(config):
86
"""Test with external service configurations"""
87
assert 'postgresql' in config['DATABASE_URL']
88
assert config['REDIS_URL'] == 'redis://localhost:6379/1'
89
90
@pytest.mark.options(mail_suppress_send=True, wtf_csrf_enabled=False)
91
def test_feature_toggles(config):
92
"""Test with feature flags and toggles"""
93
assert config['MAIL_SUPPRESS_SEND'] is True
94
assert config['WTF_CSRF_ENABLED'] is False
95
```
96
97
### Configuration in Class-Based Tests
98
99
The options marker works with class-based tests and can be applied at both class and method levels:
100
101
```python
102
@pytest.mark.options(testing=True)
103
class TestWithBaseConfig:
104
"""Base configuration applied to all test methods"""
105
106
def test_base_config(self, config):
107
assert config['TESTING'] is True
108
109
@pytest.mark.options(debug=True)
110
def test_with_additional_config(self, app, config):
111
"""Method-level options combine with class-level options"""
112
assert config['TESTING'] is True # From class
113
assert app.debug is True # From method
114
115
@pytest.mark.options(testing=False) # Override class setting
116
def test_override_class_config(self, config):
117
"""Method-level options can override class-level options"""
118
assert config['TESTING'] is False
119
```
120
121
### Dynamic Configuration
122
123
Configuration changes are applied through monkeypatching, allowing for isolated test configurations:
124
125
```python
126
def test_config_isolation(config):
127
"""Each test gets its own configuration state"""
128
original_debug = config.get('DEBUG')
129
130
# Configuration changes don't persist between tests
131
assert config['TESTING'] is True
132
133
@pytest.mark.options(custom_setting='test_value')
134
def test_custom_configuration(config):
135
"""Custom configuration values can be set"""
136
assert config['CUSTOM_SETTING'] == 'test_value'
137
138
def test_config_not_affected_by_previous_test(config):
139
"""Previous test's custom config doesn't affect this test"""
140
assert 'CUSTOM_SETTING' not in config
141
```