|
| 1 | +--- |
| 2 | +title: "Flask วิธี Test WebSocket ของ Flask-SocketIO" |
| 3 | +description: "วิธีการ Test SocketIO ของ Flask ด้วย Pytest" |
| 4 | +pubDate: "Sep 12 2025" |
| 5 | +heroImage: "../../assets/blog/flask-socketio-test/thumbnail.png" |
| 6 | +tags: ["python", "flask", "Flask-SocketIO", "SocketIO", "pytest"] |
| 7 | +--- |
| 8 | + |
| 9 | +พอดีได้เอา Project ที่เขียนด้วย Flask Python แล้วอยากทำ Test เพิ่มด้วย |
| 10 | + |
| 11 | +ใน Project มี Websocket ที่ใช้ Flask-SocketIO แล้วมันเทสยังไงกันนะ |
| 12 | + |
| 13 | +ผมไปงมหามาให้แล้ว |
| 14 | + |
| 15 | +<br> |
| 16 | + |
| 17 | +## Talk is cheap. Show me the code |
| 18 | + |
| 19 | +<br> |
| 20 | + |
| 21 | +เริ่มต้นด้วยการสร้าง Flask + SocketIO แบบง่ายๆ `main.py` |
| 22 | + |
| 23 | +```py |
| 24 | +from flask import Flask, render_template |
| 25 | +from flask_socketio import SocketIO, send |
| 26 | + |
| 27 | +socketio = SocketIO() |
| 28 | + |
| 29 | +app = Flask(__name__) |
| 30 | +app.config["SECRET_KEY"] = "secret!" |
| 31 | + |
| 32 | +@socketio.on("message") |
| 33 | +def handle_message(data): |
| 34 | + print("server socketio received message: " + data) |
| 35 | + send(f"server got: {data}") |
| 36 | + |
| 37 | +@socketio.on_error() |
| 38 | +def error_handler(e): |
| 39 | + print("socketio error:", e) |
| 40 | + |
| 41 | +@app.route("/") |
| 42 | +def hello_world(): |
| 43 | + return "Hello, World!" |
| 44 | + |
| 45 | +def create_app(): |
| 46 | + socketio.init_app(app) |
| 47 | + return app |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + app = create_app() |
| 51 | + socketio.run(app) |
| 52 | +``` |
| 53 | + |
| 54 | +แล้วสร้าง pytest test file `test_main.py` |
| 55 | + |
| 56 | +```py |
| 57 | +import pytest |
| 58 | +from main import create_app, socketio |
| 59 | +from flask.testing import FlaskClient |
| 60 | +from flask_socketio.test_client import SocketIOTestClient |
| 61 | + |
| 62 | +@pytest.fixture() |
| 63 | +def app(): |
| 64 | + app = create_app() |
| 65 | + app.config.update({"TESTING": True}) |
| 66 | + yield app |
| 67 | + |
| 68 | +@pytest.fixture() |
| 69 | +def flask_client(app): |
| 70 | + return app.test_client() |
| 71 | + |
| 72 | +@pytest.fixture() |
| 73 | +def runner(app): |
| 74 | + return app.test_cli_runner() |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +ต่อมาสร้าง SocketIO test client ด้วย `socketio.test_client(app)` |
| 79 | + |
| 80 | +ตัว `socketio.test_client(app)` ต้องใช้ `app` ที่สร้างจาก pytest fixture ข้างบน |
| 81 | + |
| 82 | +```py |
| 83 | +@pytest.fixture() |
| 84 | +def socketio_client(app): |
| 85 | + socketio_client = socketio.test_client(app) |
| 86 | + return socketio_client |
| 87 | +``` |
| 88 | + |
| 89 | +<br> |
| 90 | + |
| 91 | +#### ⚠️ Gotcha ! |
| 92 | + |
| 93 | +`socketio` ของ `socketio.test_client(app)` คือ `socketio = SocketIO()` จาก `main.py` |
| 94 | + |
| 95 | +<br> |
| 96 | + |
| 97 | +เขียน testcase ทดสอบ SocketIO |
| 98 | + |
| 99 | +```py |
| 100 | +def test_socketio_message(socketio_client: SocketIOTestClient): |
| 101 | + socketio_client.send("hello from pytest") |
| 102 | + recv = socketio_client.get_received() |
| 103 | + print("test_socketio_message recv", recv) |
| 104 | + |
| 105 | + assert len(recv) == 1 |
| 106 | + assert recv[0]["name"] == "message" |
| 107 | + assert recv[0]["args"] == "server got: hello from pytest" |
| 108 | + assert recv[0]["namespace"] == "/" |
| 109 | +``` |
| 110 | + |
| 111 | +<br> |
| 112 | + |
| 113 | +แล้วรัน Test ด้วย |
| 114 | + |
| 115 | +```sh |
| 116 | +pytest |
| 117 | +``` |
| 118 | + |
| 119 | +<br> |
| 120 | + |
| 121 | +Done! We were finally able to test WebSocket! 🎉 |
| 122 | + |
| 123 | +<br> |
| 124 | + |
| 125 | +### Source code |
| 126 | + |
| 127 | +[https://github.com/wuttinanhi/flask-socketio-test](https://github.com/wuttinanhi/flask-socketio-test) |
0 commit comments