在 Python Pytest 穩定測試電子郵件與 OTP 驗證,無需 SDK 綁定。
於每個 Python Pytest 測試前呼叫 POST /v1/mailboxes 建立獨立收件匣,透過 Fetch SSE 等待 message.ready 後擷取驗證碼。
三步驟確定性流程
1. 每個測試建立獨立信箱
呼叫 POST /v1/mailboxes 取得專屬地址與 Bearer token。
2. 訂閱 SSE 即時事件
以 Authorization: Bearer 連線 GET /v1/mailboxes/me/events。
3. 擷取驗證碼並清理
讀取訊息內容後於 teardown 呼叫 DELETE /v1/mailboxes/me。
可直接複製的 Python Pytest 測試範本
Pythonimport json
import re
import requests
API = "https://api.inboxflick.com/v1"
def wait_for_otp(token: str, timeout: int = 30) -> str:
headers = {"Authorization": f"Bearer {token}"}
with requests.get(f"{API}/mailboxes/me/events", headers=headers, stream=True, timeout=timeout) as resp:
resp.raise_for_status()
event_name = ""
for raw_line in resp.iter_lines(decode_unicode=True):
if not raw_line:
continue
if raw_line.startswith("event:"):
event_name = raw_line.split(":", 1)[1].strip()
elif raw_line.startswith("data:") and event_name == "message.ready":
payload = json.loads(raw_line.split(":", 1)[1].strip())
msg = requests.get(
f"{API}/mailboxes/me/messages/{payload['message_id']}",
headers=headers,
timeout=10,
).json()
match = re.search(r"\b(\d{6})\b", msg["data"]["text_body"])
if match:
return match.group(1)
raise TimeoutError("OTP email not received")
def test_signup_otp(app_client):
created = requests.post(f"{API}/mailboxes", json={}, timeout=10).json()["data"]
address = created["mailbox"]["address"]
token = created["access_token"]
try:
app_client.post("/signup", json={"email": address})
otp = wait_for_otp(token)
verify = app_client.post("/verify", json={"email": address, "code": otp})
assert verify.status_code == 200
finally:
requests.delete(
f"{API}/mailboxes/me",
headers={"Authorization": f"Bearer {token}"},
timeout=10,
) 並行 CI Worker?透過 Pro API 一次建立 1–20 個信箱
升級 Pro($1.99/月或 $8.99 永久)即可呼叫批量端點並行建立最多 20 個收件匣。
POST https://api.inboxflick.com/v1/member/mailboxes/batch
Authorization: Bearer <PRO_MEMBER_JWT>
Content-Type: application/json
{ "count": 20, "is_permanent": false } 以 $8.99 永久解鎖並行 CI 郵件測試
不限每日次數、1–20 批量 API 與 Pro 專屬網域。
CI 郵件測試常見問題
Python Pytest 需要安裝額外 SDK 嗎?
不需要,原生 fetch() 或 requests 即可串接。
並行測試如何避免單 IP 限制?
使用 Pro JWT 呼叫 /v1/member/mailboxes/batch 即可。
可擷取魔術連結與 HTML 嗎?
可以,API 同時提供 text_body 與淨化後的 html_body。