or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

accept-headers.mdconfiguration.mdindex.mdlive-server.mdtest-client.md

test-client.mddocs/

0

# Test Client

1

2

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.

3

4

## Capabilities

5

6

### Test Client Fixture

7

8

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.

9

10

```python { .api }

11

@pytest.fixture

12

def client(app):

13

"""

14

A Flask test client. An instance of flask.testing.TestClient by default.

15

16

Parameters:

17

app: Flask application instance (from app fixture)

18

19

Returns:

20

TestClient: Flask test client configured for the application

21

"""

22

```

23

24

**Usage Example:**

25

26

```python

27

def test_get_request(client):

28

response = client.get('/api/users')

29

assert response.status_code == 200

30

31

def test_post_request(client):

32

data = {'name': 'John', 'email': 'john@example.com'}

33

response = client.post('/api/users', json=data)

34

assert response.status_code == 201

35

assert response.json['id'] is not None

36

37

def test_with_headers(client):

38

headers = {'Authorization': 'Bearer token123'}

39

response = client.get('/api/protected', headers=headers)

40

assert response.status_code == 200

41

```

42

43

### Class-Based Test Client

44

45

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

46

47

```python { .api }

48

@pytest.fixture

49

def client_class(request, client):

50

"""

51

Uses to set a ``client`` class attribute to current Flask test client.

52

53

Parameters:

54

request: pytest request object

55

client: Flask test client instance

56

57

Returns:

58

None (sets client attribute on test class)

59

"""

60

```

61

62

### MIME Type Parameter Fixture

63

64

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

65

66

```python { .api }

67

@pytest.fixture(params=["application/json", "text/html"])

68

def mimetype(request):

69

"""

70

Parametrized fixture providing common MIME types.

71

72

Parameters:

73

request.param: One of "application/json" or "text/html"

74

75

Returns:

76

str: MIME type string for the current test parameter

77

"""

78

```

79

80

**Usage Example:**

81

82

```python

83

def test_content_type_handling(client, mimetype):

84

"""Test endpoint handles different MIME types"""

85

headers = [("Content-Type", mimetype)]

86

data = {'test': 'data'} if mimetype == 'application/json' else 'test=data'

87

88

response = client.post('/api/data',

89

data=data if mimetype != 'application/json' else None,

90

json=data if mimetype == 'application/json' else None,

91

headers=headers)

92

assert response.status_code == 200

93

```

94

95

**Usage Example:**

96

97

```python

98

@pytest.mark.usefixtures('client_class')

99

class TestUserAPI:

100

101

def login(self, email, password):

102

"""Helper method using self.client"""

103

credentials = {'email': email, 'password': password}

104

return self.client.post('/auth/login', json=credentials)

105

106

def test_user_login(self):

107

response = self.login('user@example.com', 'password123')

108

assert response.status_code == 200

109

assert 'token' in response.json

110

111

def test_protected_route(self):

112

# Login first

113

login_response = self.login('user@example.com', 'password123')

114

token = login_response.json['token']

115

116

# Access protected route

117

headers = {'Authorization': f'Bearer {token}'}

118

response = self.client.get('/api/profile', headers=headers)

119

assert response.status_code == 200

120

```

121

122

## Enhanced Response Features

123

124

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

125

126

### JSON Response Comparison

127

128

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

129

130

```python

131

def test_json_response(client):

132

response = client.get('/api/ping')

133

134

# Status code comparison

135

assert response == 200

136

137

# JSON content access

138

assert response.json == {'ping': 'pong'}

139

140

# Traditional status code access still works

141

assert response.status_code == 200

142

assert response.status == '200 OK'

143

```

144

145

### Automatic Request Context

146

147

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

148

149

```python

150

from flask import url_for

151

152

def test_with_url_for(client):

153

# url_for works automatically in tests

154

response = client.get(url_for('api.get_user', user_id=123))

155

assert response.status_code == 200

156

157

def test_reverse_url(client):

158

# Can build URLs using Flask's routing

159

url = url_for('api.create_user')

160

response = client.post(url, json={'name': 'Jane'})

161

assert response.status_code == 201

162

```