CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pytest-flask

A set of pytest fixtures to test Flask applications.

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration

Flask application configuration access and manipulation during tests. Provides direct access to app configuration and runtime configuration changes through pytest markers.

Capabilities

Configuration Access

Direct access to the Flask application's configuration dictionary for reading and asserting configuration values during tests.

@pytest.fixture
def config(app):
    """
    An application config.
    
    Parameters:
        app: Flask application instance (from app fixture)
        
    Returns:
        Config: Flask application config object (werkzeug.datastructures.ImmutableDict)
    """

Usage Example:

def test_config_values(config):
    assert config['TESTING'] is True
    assert config['SECRET_KEY'] is not None
    assert config['DATABASE_URL'] == 'sqlite:///:memory:'

def test_debug_mode(app, config):
    # Access config through fixture
    assert config['DEBUG'] == app.debug
    
def test_environment_config(config):
    # Check environment-specific settings
    if config.get('ENV') == 'testing':
        assert config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite')
    else:
        assert config['SQLALCHEMY_DATABASE_URI'].startswith('postgresql')

Options Marker

Configure Flask application settings for specific tests using the @pytest.mark.options decorator. Options are applied to the application config before the test runs.

@pytest.mark.options(**kwargs)
def test_function():
    """
    Decorator to pass options to your application factory.
    
    Parameters:
        **kwargs: Configuration key-value pairs to set on app.config
        
    Usage:
        @pytest.mark.options(debug=False, testing=True)
        def test_something(app):
            assert not app.debug
            assert app.config['TESTING']
    """

Usage Examples:

@pytest.mark.options(debug=False)
def test_production_mode(app, config):
    """Test with debug mode disabled"""
    assert not app.debug
    assert config['DEBUG'] is False

@pytest.mark.options(testing=True, debug=True, secret_key='test-secret')
def test_with_multiple_options(app, config):
    """Test with multiple configuration options"""
    assert app.debug is True
    assert config['TESTING'] is True
    assert config['SECRET_KEY'] == 'test-secret'

@pytest.mark.options(
    database_url='postgresql://test:test@localhost/testdb',
    redis_url='redis://localhost:6379/1'
)
def test_external_services_config(config):
    """Test with external service configurations"""
    assert 'postgresql' in config['DATABASE_URL']
    assert config['REDIS_URL'] == 'redis://localhost:6379/1'

@pytest.mark.options(mail_suppress_send=True, wtf_csrf_enabled=False)
def test_feature_toggles(config):
    """Test with feature flags and toggles"""
    assert config['MAIL_SUPPRESS_SEND'] is True
    assert config['WTF_CSRF_ENABLED'] is False

Configuration in Class-Based Tests

The options marker works with class-based tests and can be applied at both class and method levels:

@pytest.mark.options(testing=True)
class TestWithBaseConfig:
    """Base configuration applied to all test methods"""
    
    def test_base_config(self, config):
        assert config['TESTING'] is True
    
    @pytest.mark.options(debug=True)
    def test_with_additional_config(self, app, config):
        """Method-level options combine with class-level options"""
        assert config['TESTING'] is True  # From class
        assert app.debug is True          # From method
        
    @pytest.mark.options(testing=False)  # Override class setting
    def test_override_class_config(self, config):
        """Method-level options can override class-level options"""
        assert config['TESTING'] is False

Dynamic Configuration

Configuration changes are applied through monkeypatching, allowing for isolated test configurations:

def test_config_isolation(config):
    """Each test gets its own configuration state"""
    original_debug = config.get('DEBUG')
    
    # Configuration changes don't persist between tests
    assert config['TESTING'] is True

@pytest.mark.options(custom_setting='test_value')
def test_custom_configuration(config):
    """Custom configuration values can be set"""
    assert config['CUSTOM_SETTING'] == 'test_value'

def test_config_not_affected_by_previous_test(config):
    """Previous test's custom config doesn't affect this test"""
    assert 'CUSTOM_SETTING' not in config

Install with Tessl CLI

npx tessl i tessl/pypi-pytest-flask

docs

accept-headers.md

configuration.md

index.md

live-server.md

test-client.md

tile.json