设置
从 Python 文档 把辅助工具 proxymail_qa.py 复制到测试目录,并把 API 密钥 放进 PROXYMAIL_QA_API_KEY:可以在 shell 中、不提交到 Git 的 .env 文件中,或 CI 的 secrets 中。免费套餐就够了。
测试
使用适用于 Python 的 Playwright:
import re
from playwright.sync_api import Page, expect
from proxymail_qa import create_address, delete_address, now, wait_for_link
def test_sign_up_verifies_the_email(page: Page): account = create_address() page.goto("/signup") page.get_by_label("Email").fill(account["address"]) page.get_by_label("Password").fill(account["password"])
submitted = now() page.get_by_role("button", name="Sign up").click()
page.goto(wait_for_link(account["address"], since=submitted)) expect(page).to_have_url(re.compile("verified")) delete_address(account["address"])如果是验证码,使用 wait_for_code(address, since=submitted) 并输入它返回的内容。用 Selenium 时调用方式相同:用 driver.find_element 找到字段,发送验证码或打开链接。since 让等待忽略表单提交前发出的所有邮件,所以重试的测试不会读到旧邮件。
检查邮件本身,而不只是链接
import time
from proxymail_qa import check_email, count_emails, now, wait_for_link
def test_sign_up_sends_one_good_verification_email(page, account): submitted = now() page.get_by_role("button", name="Sign up").click() wait_for_link(account["address"], since=submitted) time.sleep(10) # give a duplicate time to arrive assert count_emails(account["address"], since=submitted, type="link")["count"] == 1 assert check_email(account["address"], since=submitted, expect_host="staging.example.com")["failed"] == []check_email 返回关于 SPF、DKIM 和 DMARC 的检查结果、指向 localhost 或纯 http 的链接、链接的去向等,从不返回邮件本身。expect_no_email 检查某个操作没有发出任何邮件,比如应被拒绝的第二次重置请求。
在 CI 中运行,并在 pull request 上生成报告
在 conftest.py 中导入辅助工具的 pytest hooks。在 GitHub Actions 中,在测试前添加 Proxy Mail QA action:pull request 上的报告会说明每封邮件来自哪个测试,并列出失败的测试及其邮件情况。
from proxymail_qa import pytest_runtest_setup, pytest_runtest_teardown, pytest_runtest_logreport, pytest_sessionfinish - uses: aaquib90/proxymail-qa-action@v1 with: api-key: ${{ secrets.PROXYMAIL_QA_API_KEY }} - run: pytest env: PROXYMAIL_QA_API_KEY: ${{ secrets.PROXYMAIL_QA_API_KEY }}