開発者が使い捨てメールをテストに活用する方法
メール機能のテストは面倒です。Gmailアカウント#47を作成し、確認し、受信トレイをチェックし、メールのスクリーンショットを撮り、テストごとに繰り返す。
使い捨てメールがこれを変えます:アドレス生成、フローテスト、タブを閉じる。クリーンアップゼロ、手動作業ゼロ。
テストの問題
従来のメールテストワークフロー:
1. [email protected] でテストユーザーを作成
2. パスワードリセットをトリガー
3. 別タブでGmailを開く
4. メールを待つ
5. 200通の他のテストメールの中から見つける
6. テスト完了
7. 受信トレイに247通のテストメール
問題点:
- 受信トレイが汚れる
- 並列化できない(同じアドレスが再利用される)
- Gmailがテストメールをスパムとしてマークする可能性
- 数百のテストアカウントを手動でクリーンアップする必要がある
ソリューション:使い捨てメールAPI
tempy.emailを使って:
// メールボックスを作成
const mailbox = await fetch('https://tempy.email/api/v1/mailbox', {
method: 'POST'
}).then(r => r.json());
// テストで使用
await yourApp.signup({ email: mailbox.email });
// メールを取得
const emails = await fetch(
`https://tempy.email/api/v1/mailbox/${mailbox.email}/emails`
).then(r => r.json());
console.log(emails[0].subject); // "メールアドレスを確認してください"
// アドレスは自動的に期限切れ - クリーンアップ不要
実際のユースケース
1. 統合テスト
def test_welcome_email():
# 一時メールボックスを生成
r = requests.post('https://tempy.email/api/v1/mailbox')
email = r.json()['email']
# ユーザーを作成
create_user(email=email)
# 受信トレイを確認
time.sleep(2)
emails = requests.get(f'https://tempy.email/api/v1/mailbox/{email}/emails')
assert "Welcome" in emails.json()[0]['subject']
2. E2Eテスト(Cypress/Playwright)
// テスト実行ごとにユニークなメールを生成
const mailbox = await cy.request('POST', 'https://tempy.email/api/v1/mailbox');
cy.visit('/signup');
cy.get('[name="email"]').type(mailbox.body.email);
cy.get('button').click();
// 確認メールを待つ
cy.wait(2000);
cy.request(`https://tempy.email/api/v1/mailbox/${mailbox.body.email}/emails`)
.then((res) => {
const link = extractLink(res.body[0].bodyHtml);
cy.visit(link);
});
3. 負荷テスト
1000件の登録を並列テスト — それぞれユニークなアドレス:
await Promise.all(
Array(1000).fill().map(async () => {
const m = await createMailbox();
await testSignup(m.email);
// 自動的に期限切れ、クリーンアップ不要
})
);
4. メールテンプレートの検証
// テストメールを送信
await sendEmail({
to: tempAddress,
template: 'order_confirmation',
data: { order: '12345', total: '$99' }
});
// テンプレートが正しくレンダリングされたか確認
const email = await getEmail(tempAddress);
assert(email.bodyHtml.includes('Order #12345'));
assert(email.bodyHtml.includes('$99'));
APIリファレンス
メールボックスの作成:
POST https://tempy.email/api/v1/mailbox
→ { "email": "[email protected]", "expires_at": "..." }
メールの取得:
GET https://tempy.email/api/v1/mailbox/{address}/emails
→ [{ "subject": "...", "bodyHtml": "...", ... }]
TTLの延長:
POST https://tempy.email/api/v1/mailbox/{address}/extend
→ 5分追加
ベストプラクティス
テストごとにユニークなアドレスを使用:
// 各テストがクリーンな受信トレイを取得
test('signup', async () => {
const email = await createUniqueEmail();
await testWith(email);
});
メールをポーリング:
async function pollForEmail(address, timeout = 10000) {
const start = Date.now();
while (Date.now() - start < timeout) {
const emails = await getEmails(address);
if (emails.length > 0) return emails[0];
await sleep(500);
}
throw new Error('メールが受信されませんでした');
}
共有アドレスを使わない:
// 悪い例 - 異なるテストのメールが混ざる
const SHARED_EMAIL = '[email protected]';
時間の節約
以前: 10分/テスト x 100テスト/週 = 1000分/週 = 16.7時間/週
以後: 30秒/テスト x 100テスト = 50分/週
節約:開発者一人あたり週15時間以上。
CI/CD統合
# .github/workflows/test.yml
name: Email Tests
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: npm test -- email.test.js
env:
TEMP_EMAIL_API: https://tempy.email/api/v1
APIキー不要。レート制限なし。そのまま動きます。
受信トレイを汚すのはやめましょう。tempy.email でもっとスマートにテストしましょう。