[英]Python 2.7 and pip install realted issues with _markerlib and installing other packages failures
[英]How to auto email test failures with pytest or other python packages?
我有一个页面并测试 class,测试 class 如下所示
import unittest
import pytest
import logging
@pytest.mark.usefixtures("setup")
class BigRandomTest(unittest.TestCase):
log = cl.testogger(logging.DEBUG)
@pytest.fixture(autouse=True)
def classSetup(self):
self.synMax = CarMaxSyndicated()
# Standard set of tests
@pytest.mark.run(order=1)
def test_column_uniqueness_dealership(self):
pass
self.log.debug('List of all column duplicate counts: \n' + str(result[1]))
assert result[0].empty, 'Test failed. Columns with a suspect amount of duplicates: \n {}'.format(result[0])
@pytest.mark.run(order=2)
def test_column_uniqueness_consistency_dealership(self):
pass
self.log.debug('List of all column duplicates differences: \n' + str(result[1]))
assert result[0].empty, 'Test failed. Columns with significantly different duplicate counts between collections: \n {}'.format(result[0])
当我运行 pytest -s -v path_to_test --html=result.html --self-contained-html 它会生成报告,但我想要做的是 email 报告,只要只有测试失败,我可以设置一些stmp 或者如果有一个 package 我可以使用它会很有帮助。
以下是 conftest.py 的完整代码在这种情况下的样子:
比赛.py
import pytest
import smtplib
import ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
failed: bool = False
def send_email():
"""
function to send an email with attachment after all test are run and a failure was detected
"""
subject: str = "subject"
body: str = "body"
sender_email: str = "email"
receiver_email: str = "email"
password: str = "password" # Recommended for mass emails# This should obviously be an input or some other stronger protection object other than a string
smtp: str = "smtp"
filename: str = "result.html"
port: int = 587
# Attachments need to be multipart
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email
# Add body to email
message.attach(MIMEText(body, "plain"))
# Open html file in binary mode
# This will only work if HTML file is located within the same dir as the script
# If it is not, then you will need to modify this to the right path
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP(smtp, port) as server:
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
global failed
report = yield
result = report.get_result()
if result.when == 'call' and result.outcome == "failed":
failed = True
print("FAILED")
def pytest_sessionfinish(session, exitstatus):
if failed is True:
send_email()
该文件中有两个关键函数。
pytest_runtest_makereport 将在每次测试后运行,并简单地检查测试是否失败。 如果任何测试失败,则failed
标志将设置为 True。
然后在整个脚本结束时,pytest_session 将运行并检查标志是否已更改为 True。 如果是,则将发送 email 并附上 result.html 文件。
我想强调这个问题的一部分之前已经回答过,请参阅下面的链接: How to send pytest coverage report via email?
鉴于链接的问题/答案不包含有关如何仅在测试失败时运行的任何信息。 我认为值得在这里回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.