Kiểm thử xác minh email là một trong những khía cạnh nhàm chán nhất nhưng quan trọng nhất của phát triển phần mềm. Mọi quy trình đăng ký, mọi email giao dịch, và mọi thông báo đều cần được kiểm thử kỹ lưỡng. Địa chỉ email dùng một lần biến quá trình này từ một tác vụ thủ công mệt mỏi thành một pipeline tự động có khả năng mở rộng.
Thiết Lập Kiểm Thử API
Bước đầu tiên là thiết lập môi trường kiểm thử của bạn với API email dùng một lần. Bạn cần các endpoint để tạo địa chỉ email mới, kiểm tra hộp thư đến, và lấy nội dung email. Với EvilMail API, tất cả đều có sẵn thông qua các REST endpoint đơn giản và được tài liệu hóa đầy đủ.
Bắt đầu bằng cách tạo địa chỉ email kiểm thử thông qua API:
curl -X POST https://api.evilmail.com/v1/addresses
-H "Authorization: Bearer YOUR_API_KEY"
-H "Content-Type: application/json"
-d '{"prefix": "test-user", "domain": "evilmail.com"}'Ví Dụ Python Cho Kiểm Thử Tự Động
Dưới đây là ví dụ đầy đủ sử dụng Python để tự động hóa kiểm thử xác minh email:
import requests
import time
import re
class EmailVerificationTester:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "https://api.evilmail.com/v1"
self.headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
def create_test_address(self, prefix="test"):
response = requests.post(
f"{self.base_url}/addresses",
headers=self.headers,
json={"prefix": prefix, "domain": "evilmail.com"}
)
return response.json()["address"]
def wait_for_email(self, address, timeout=30):
start = time.time()
while time.time() - start < timeout:
response = requests.get(
f"{self.base_url}/inboxes/{address}/messages",
headers=self.headers
)
messages = response.json()["messages"]
if messages:
return messages[0]
time.sleep(2)
raise TimeoutError("Không nhận được email trong thời gian chờ")
def extract_verification_link(self, message_id, address):
response = requests.get(
f"{self.base_url}/inboxes/{address}/messages/{message_id}",
headers=self.headers
)
body = response.json()["body"]
links = re.findall(r'https?://[^s<>"]+verify[^s<>"]*', body)
return links[0] if links else None
