from flask import Flask, request app = Flask(__name__) @app.route('/login', methods=['POST']) def login(): username = request.form['username'] password = request.form['password'] if username == 'admin' and password == 'secret': return 'Login successful!' else: return 'Invalid credentials' app.run()
from my_app import app from flask.testing import FlaskClient def test_login(): client = FlaskClient(app) data = { 'username': 'admin', 'password': 'secret' } response = client.post('/login', data=data) assert response.status_code == 200 assert b"Login successful!" in response.dataIn this test case, we create a FlaskClient object with our Flask app and send a POST request to the login route with proper credentials to login successfully. The package/library used in this example is `flask.testing`.