Complete worked example
python
from flask import Flask, url_for
app = Flask(__name__)
app.config['TESTING'] = True
@app.get('/roles/<int:role_id>')
def detail(role_id):
return {'id': role_id}
def visit(role_id, method='GET'):
if type(role_id) is not int or role_id < 0:
raise ValueError('nonnegative integer required')
with app.test_request_context():
path = url_for('detail', role_id=role_id)
response = app.test_client().open(path, method=method)
return {'path': path, 'status': response.status_code}- Reasoning
- The example validates the helper input, opens a short request context for URL generation and sends a request through the test client. The view permits GET, with Flask providing normal HEAD and OPTIONS behavior.