pytest+allure发送带附件的邮件
时间: 2024-12-21 12:28:48 浏览: 40
pytest是一个流行的Python测试框架,而Allure是一种用于生成测试报告的工具,它可以帮助你创建美观、结构化的测试结果。将它们结合起来可以让你在测试完成之后自动发送包含Allure报告作为附件的邮件通知。
首先,你需要安装pytest和allure-pytest插件,以及一个用于发送邮件的库,如smtplib或者anymail。以下是简单的步骤:
1. 安装必要的依赖:
```
pip install pytest allure pytest-html anymail
```
2. 配置pytest-allure-reporter,生成HTML报告:
```python
pytest --html=allure_report.html
```
这会在`allure_report.html`文件中保存Allure报告。
3. 使用anymail或smtplib设置邮件发送配置,并在测试结束后添加代码发送邮件,包括报告作为附件:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# ...在pytest运行结束后...
def send_email_with_attachment():
# 你的邮箱配置
sender = '[email protected]'
receiver = '[email protected]'
password = 'your-password'
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = "Pytest Allure Report"
with open('allure_report.html', 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype="html")
attachment.add_header('Content-Disposition', 'attachment', filename='allure_report.html')
msg.attach(attachment)
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login(sender, password)
text = "Please find attached the pytest Allure report."
msg.attach(MIMEText(text, 'plain'))
server.send_message(msg)
server.quit()
send_email_with_attachment()
```
阅读全文
相关推荐


















