要结合多系统并存的架构,使用 YAML 文件编写测试用例,并集成钉钉、邮件通知功能以及 CI/CD 流程,以下是完整的实现方案。
整体功能架构
- 多系统测试支持:
- 使用 YAML 文件定义测试用例,支持多系统间的模块化、分层管理。
- 测试框架根据 YAML 文件动态加载测试用例,支持灵活扩展。
- 测试报告生成与通知:
- 测试完成后生成 HTML 测试报告。
- 使用钉钉机器人推送总结消息。
- 通过邮件发送详细的测试报告。
- CI/CD 集成:
- 配置 CI/CD 流程,在代码变更后自动执行测试用例。
- 自动生成报告并发送通知。
- 部署系统或服务到目标环境。
详细实现步骤
1. YAML 文件定义测试用例
使用 YAML 文件来组织测试用例,以支持多系统并存的场景。以下是一个示例 YAML 文件:
# tests/system_tests.yml
system_a:
- name: "Test API endpoint A1"
method: "GET"
url: "https://system-a.example.com/api/v1/resource"
expected_status: 200
expected_response: {"success": true}
- name: "Test API endpoint A2"
method: "POST"
url: "https://system-a.example.com/api/v1/resource"
payload: {"key": "value"}
expected_status: 201
system_b:
- name: "Test API endpoint B1"
method: "GET"
url: "https://system-b.example.com/api/v1/resource"
expected_status: 200
expected_response: {"success": true}
2. Python 脚本动态加载 YAML 用例并执行
编写 Python 脚本,动态加载 YAML 文件并执行用例。
import yaml
import requests
import pytest
# 加载 YAML 文件
def load_test_cases(yaml_file):
with open(yaml_file, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
# 定义测试函数
def run_test_case(test_case):
method = test_case.get('method', 'GET').upper()
url = test_case['url']
payload = test_case.get('payload', {})
headers = test_case.get('headers', {})
expected_status = test_case.get('expected_status')
expected_response = test_case.get('expected_response')
# 发起请求
response = requests.request(method, url, json=payload, headers=headers)
# 验证结果
assert response.status_code == expected_status, f"Expected {expected_status}, got {response.status_code}"
if expected_response:
assert response.json() == expected_response, f"Expected {expected_response}, got {response.json()}"
# 动态生成 pytest 测试用例
def pytest_generate_tests(metafunc):
if "test_case" in metafunc.fixturenames:
test_cases = []
data = load_test_cases("tests/system_tests.yml")
for system, cases in data.items():
for case in cases:
case["system"] = system
test_cases.append(case)
metafunc.parametrize("test_case", test_cases)
# 测试入口
@pytest.mark.parametrize("test_case", [])
def test_dynamic_cases(test_case):
run_test_case(test_case)
3. 集成测试报告生成与通知
测试完成后,生成 HTML 报告,并通过钉钉和邮件发送通知。
钉钉通知
使用钉钉机器人接口推送测试结果。
import requests
import json
def send_dingtalk_message(webhook_url, message):
headers = {"Content-Type": "application/json"}
payload = {
"msgtype": "text",
"text": {
"content": message
}
}
response = requests.post(webhook_url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
print("钉钉消息发送成功")
else:
print("钉钉消息发送失败: ", response.text)
邮件通知
通过 SMTP 发送邮件,附带 HTML 报告。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
def send_email(smtp_server, port, sender_email, sender_password, recipient_email, subject, body, attachment_path=None):
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
if attachment_path and os.path.exists(attachment_path):
with open(attachment_path, 'rb') as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(attachment_path)}')
msg.attach(part)
with smtplib.SMTP(smtp_server, port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
print("邮件发送成功")
4. CI/CD 配置
以 GitHub Actions 为例,自动化测试与部署。
name: Multi-System Testing and Deployment
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install pytest pytest-html requests pyyaml
- name: Run tests
run: |
pytest --html=report.html --self-contained-html
- name: Send DingTalk Notification
run: |
python -c "
from notify import send_dingtalk_message;
send_dingtalk_message('https://oapi.dingtalk.com/robot/send?access_token=xxxxxx', '测试已完成,请查看报告。')"
- name: Send Email Notification
run: |
python -c "
from notify import send_email;
send_email('smtp.example.com', 587, 'your_email@example.com', 'your_password', 'recipient@example.com', '测试报告', '测试已完成,报告见附件。', 'report.html')"
deploy:
runs-on: ubuntu-latest
needs: test
steps:
- name: Deploy to server
run: echo '执行部署逻辑,例如通过 SSH、Docker 或 Kubernetes 部署'
总结
- 多系统支持:
- 使用 YAML 文件定义测试用例,支持多系统的模块化管理。
- 动态加载测试用例,减少代码重复性。
- 报告与通知:
- 测试完成后生成 HTML 报告。
- 钉钉消息推送测试结果。
- 邮件发送详细报告。
- 自动化 CI/CD:
- 使用 GitHub Actions 实现自动化测试与部署流程。
- 支持代码变更后自动触发测试、通知和部署。
这套流程适用于多系统并存的测试场景,具有较高的灵活性和可扩展性。
标签:集成,case,response,expected,pytest,test,import,email,邮件 From: https://blog.csdn.net/weixin_44872675/article/details/145189855