CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pytest-flask

A set of pytest fixtures to test Flask applications.

Pending
Overview
Eval results
Files

test-client.mddocs/

Test Client

Flask test client fixtures for HTTP request testing. Provides access to Flask's built-in test client with automatic application context management and enhanced response handling.

Capabilities

Test Client Fixture

Provides a Flask test client instance for making HTTP requests to the application during tests. The client is automatically configured with the test application and manages request contexts.

@pytest.fixture
def client(app):
    """
    A Flask test client. An instance of flask.testing.TestClient by default.
    
    Parameters:
        app: Flask application instance (from app fixture)
        
    Returns:
        TestClient: Flask test client configured for the application
    """

Usage Example:

def test_get_request(client):
    response = client.get('/api/users')
    assert response.status_code == 200
    
def test_post_request(client):
    data = {'name': 'John', 'email': 'john@example.com'}
    response = client.post('/api/users', json=data)
    assert response.status_code == 201
    assert response.json['id'] is not None

def test_with_headers(client):
    headers = {'Authorization': 'Bearer token123'}
    response = client.get('/api/protected', headers=headers)
    assert response.status_code == 200

Class-Based Test Client

Sets the test client as a class attribute for test classes, enabling shared client access across test methods within a class.

@pytest.fixture
def client_class(request, client):
    """
    Uses to set a ``client`` class attribute to current Flask test client.
    
    Parameters:
        request: pytest request object
        client: Flask test client instance
        
    Returns:
        None (sets client attribute on test class)
    """

MIME Type Parameter Fixture

Provides parametrized MIME types for testing content negotiation. This is used internally by accept_mimetype but can be used independently.

@pytest.fixture(params=["application/json", "text/html"])
def mimetype(request):
    """
    Parametrized fixture providing common MIME types.
    
    Parameters:
        request.param: One of "application/json" or "text/html"
        
    Returns:
        str: MIME type string for the current test parameter
    """

Usage Example:

def test_content_type_handling(client, mimetype):
    """Test endpoint handles different MIME types"""
    headers = [("Content-Type", mimetype)]
    data = {'test': 'data'} if mimetype == 'application/json' else 'test=data'
    
    response = client.post('/api/data', 
                          data=data if mimetype != 'application/json' else None,
                          json=data if mimetype == 'application/json' else None,
                          headers=headers)
    assert response.status_code == 200

Usage Example:

@pytest.mark.usefixtures('client_class')
class TestUserAPI:
    
    def login(self, email, password):
        """Helper method using self.client"""
        credentials = {'email': email, 'password': password}
        return self.client.post('/auth/login', json=credentials)
    
    def test_user_login(self):
        response = self.login('user@example.com', 'password123')
        assert response.status_code == 200
        assert 'token' in response.json
        
    def test_protected_route(self):
        # Login first
        login_response = self.login('user@example.com', 'password123')
        token = login_response.json['token']
        
        # Access protected route
        headers = {'Authorization': f'Bearer {token}'}
        response = self.client.get('/api/profile', headers=headers)
        assert response.status_code == 200

Enhanced Response Features

The test client automatically provides enhanced response objects with additional JSON testing capabilities:

JSON Response Comparison

Response objects can be compared directly with HTTP status codes and include automatic JSON parsing:

def test_json_response(client):
    response = client.get('/api/ping')
    
    # Status code comparison
    assert response == 200
    
    # JSON content access
    assert response.json == {'ping': 'pong'}
    
    # Traditional status code access still works
    assert response.status_code == 200
    assert response.status == '200 OK'

Automatic Request Context

The test client automatically pushes Flask request contexts during tests, enabling use of Flask utilities:

from flask import url_for

def test_with_url_for(client):
    # url_for works automatically in tests
    response = client.get(url_for('api.get_user', user_id=123))
    assert response.status_code == 200
    
def test_reverse_url(client):
    # Can build URLs using Flask's routing
    url = url_for('api.create_user')
    response = client.post(url, json={'name': 'Jane'})
    assert response.status_code == 201

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