Python Pytest で sleep() なし・低コストに OTP メール認証を自動テスト。
各 Python Pytest テストで POST /v1/mailboxes から独立した一時メールを発行し、Fetch SSE で message.ready を待機して OTP を抽出できます。
3ステップの決定的ワークフロー
1. テストごとに受信箱を作成
POST /v1/mailboxes で一意のアドレスと Bearer access_token を取得します。
2. SSE で即時待機
GET /v1/mailboxes/me/events を購読し message.ready イベントを受け取ります。
3. OTP 抽出と削除
GET /v1/mailboxes/me/messages/{id} を読み取り、最後に DELETE /v1/mailboxes/me を呼びます。
コピーして使える Python Pytest OTP テストコード
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 実行には Pro の 1〜20件一括 API を活用
Pro(月額 $1.99 / 買い切り $8.99)なら 1回の API 呼び出しで最大20件の並列受信箱と専用隔離ドメインを確保できます。
POST https://api.inboxflick.com/v1/member/mailboxes/batch
Authorization: Bearer <PRO_MEMBER_JWT>
Content-Type: application/json
{ "count": 20, "is_permanent": false } 並列 CI メールテストを買い切り $8.99 で解放
1日の上限なし・1〜20一括 API・専用ドメイン・10,000件永久保持。
CI メールテスト FAQ
Python Pytest 専用の SDK パッケージは必要ですか?
不要です。標準の fetch() や Python requests だけで動作します。
並列テストでゲスト制限を回避するには?
Pro JWT を使って POST /v1/member/mailboxes/batch を呼び出してください。
マジックリンクや HTML メールも取得できますか?
はい、text_body とサニタイズ済み html_body の両方を返します。