我现在是python写的代码通过Jenkins运行后生成的allure的测试报告,但是Jenkins是本地的通过邮件把allure测试报告的地址发送出去后是访问不了的,所以我现在想在原来基础上用pytest-testreport生成一个html的测试报告文件后在通过Jenkins邮件发送出去。
这是我准备使用再用例下生成html格式的测试报告命令,html报告存放的地方怎么指定在哪个文件夹:
import pytest
pytest.main(['--report=musen.html',
'--title=柠檬班上课报告',
'--tester=测试员',
'--desc=报告描述信息',
'--template=2'])
这个是我在Jenkins上使用的groovy邮件模板,能不能再这个模板上添加一个发送html文件测试报告的代码,让每次执行完用例把最新的html格式的测试报告通过邮件发送出去,还有Jenkins该怎么配置请详细指导一下我这个newcomer。
html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<style type="text/css">
/*base css*/
body
{
margin: 0px;
padding: 15px;
}
body, td, th
{
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Tahoma, sans-serif;
font-size: 10pt;
}
th
{
text-align: left;
}
h1
{
margin-top: 0px;
}
a
{
color:#4a72af
}
/*div styles*/
.status{background-color:<%=
build.result.toString() == "SUCCESS" ? 'green' : 'red' %>;font-size:28px;font-weight:bold;color:white;width:720px;height:52px;margin-bottom:18px;text-align:center;vertical-align:middle;border-collapse:collapse;background-repeat:no-repeat}
.status .info{color:white!important;text-shadow:0 -1px 0 rgba(0,0,0,0.3);font-size:32px;line-height:36px;padding:8px 0}
style>
<body>
<div class="content round_border">
<div class="status">
<p class="info">The build <%= build.result.toString().toLowerCase() %>p>
div>
<table>
<tbody>
<tr>
<th>项目名称:th>
<td>${project.name}td>
tr>
<tr>
<th>触发原因:th>
<%
cause = build.getCause(hudson.model.Cause.UserIdCause.class)
if (cause != null) {
user_name = cause.getUserName()
} else {
user_name = 'fuhao'
}
%>
<td>Started by ${user_name} td>
tr>
<tr>
<th>构建编号 ${build.displayName}:th>
<td><a
href="${rooturl}${build.url}">${rooturl}${build.url}a>td>
tr>
<tr>
<th>构建时间:th>
<td>${it.timestampString}td>
tr>
<tr>
<th>构建耗时:th>
<td>${build.durationString}td>
tr>
<tr>
<td colspan="2"> td>
tr>
tbody>
table>
<% def artifacts = build.artifacts
if(artifacts != null && artifacts.size() > 0) { %>
<b>离线报告:b>
<ul>
<% artifacts.each() { f -> %>
<li><a href="${rooturl}${build.url}artifact/${f}">${f}a>li>
<% } %>
ul>
<% } %>
<%
lastAllureReportBuildAction = build.getAction(ru.yandex.qatools.allure.jenkins.AllureReportBuildAction.class)
lastAllureBuildAction = build.getAction(ru.yandex.qatools.allure.jenkins.AllureBuildAction.class)
if (lastAllureReportBuildAction) {
allureResultsUrl = "${rooturl}${build.url}allure"
allureLastBuildSuccessRate = String.format("%.2f", lastAllureReportBuildAction.getPassedCount() * 100f / lastAllureReportBuildAction.getTotalCount())
}
%>
<% if (lastAllureReportBuildAction) { %>
<h2>测试结果h2>
<table>
<tbody>
<tr>
<th>执行用例数:th>
<td><a href="${allureResultsUrl}">${lastAllureReportBuildAction.getTotalCount()}a>td>
tr>
<tr>
<th>失败:th>
<td>${lastAllureReportBuildAction.getFailedCount()} td>
tr>
<tr>
<th>成功:th>
<td>${lastAllureReportBuildAction.getPassedCount()} td>
tr>
<tr>
<th>跳过:th>
<td>${lastAllureReportBuildAction.getSkipCount()} td>
tr>
<tr>
<th>故障:th>
<td>${lastAllureReportBuildAction.getBrokenCount()} td>
tr>
<tr>
<th>通过率: th>
<td>${allureLastBuildSuccessRate}% td>
tr>
tbody>
table>
<%
String auth = "jeevan" + ":" + "Fuhaoking7"; byte[] encodedAuth = auth.bytes.encodeBase64().toString(); String authHeaderValue = "Basic " + new String(encodedAuth);
content=new URL("${allureResultsUrl}/graph").getBytes( useCaches: true, allowUserInteraction: false, requestProperties: ["User-Agent": "Groovy Sample Script","Authorization": authHeaderValue])
%>
<img src="data:image/png;base64, ${content.encodeBase64().toString()}"/>
<% } %>
body>
参考gpt和自己的思路,要将pytest-testreport生成的HTML测试报告作为附件发送到Jenkins邮件,您可以使用Python中的smtplib和email模块来发送电子邮件。以下是一些步骤和示例代码:
安装email和pytest-testreport模块
pip install email pytest-testreport
导入必要的模块:
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from pytest_testreport.plugin import pytest_html_results_table_header, pytest_html_results_table_row, pytest_html_results_table_footer
定义一个函数来生成测试报告:
def generate_test_report():
pytest.main(['--html=report.html'])
定义一个函数来发送电子邮件,并将测试报告作为附件:
def send_email():
# 配置邮件参数
from_address = 'from@example.com'
to_address = 'to@example.com'
password = 'password'
subject = 'Test Report'
body = 'Please find the attached test report.'
file_path = 'report.html'
# 创建一个多部分邮件实例
message = MIMEMultipart()
message['From'] = from_address
message['To'] = to_address
message['Subject'] = subject
# 添加正文部分
message.attach(MIMEText(body, 'plain'))
# 添加测试报告附件
with open(file_path, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='html')
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_path))
message.attach(attachment)
# 发送邮件
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_address, password)
text = message.as_string()
server.sendmail(from_address, to_address, text)
server.quit()
在测试用例文件中添加以下代码来定义测试报告表格的标题和脚注:
def pytest_html_results_table_header(cells):
cells.insert(2, html.th('Description'))
cells.pop()
def pytest_html_results_table_row(report, cells):
cells.insert(2, html.td(report.description))
cells.pop()
def pytest_html_results_table_footer():
pass
在Jenkins中配置一个构建任务,使其可以自动运行您的测试用例并在测试完成后执行send_email函数。
希望这可以帮助您。
参考GPT和自己的思路,要将pytest-testreport生成的HTML测试报告作为附件通过Jenkins邮件发送,可以使用Jenkins的Email Extension插件。该插件可以在Jenkins作业执行后发送电子邮件,包括作业构建的状态、附件等信息。
以下是步骤:
1.安装“Email Extension”插件:在Jenkins管理页面中的“插件管理”部分搜索并安装该插件。
2.在Jenkins作业中配置邮件通知:在作业配置页面的“Post-build Actions”部分选择“Editable Email Notification”,配置邮件发送相关信息,例如收件人、主题、邮件正文等。注意,要在邮件正文中添加HTML格式的测试报告作为附件。
3.生成HTML测试报告:在pytest测试命令中使用pytest-html插件生成HTML测试报告,并将其保存在指定目录下。例如:
pytest --html=report.html --self-contained-html
--html 参数指定要生成的HTML测试报告的文件名,--self-contained-html 参数指定将测试报告中的CSS和JavaScript样式等嵌入到HTML文件中。
注意:如果在pytest命令中使用了--html参数,可以不再使用pytest-testreport。
4.在邮件正文中添加HTML测试报告作为附件:在“Editable Email Notification”配置页面中,选择“Advanced Settings”,在“Attachments”部分中添加测试报告的文件路径(例如:${WORKSPACE}/report.html)。
5.运行Jenkins作业并查看邮件:运行Jenkins作业,等待邮件通知,查看邮件附件是否包含HTML测试报告。
关于Jenkins配置,请确保以下几点:
1.确认Jenkins服务器可以访问HTML测试报告的存储路径,例如${WORKSPACE}/report.html。
2.在“Editable Email Notification”配置页面的“Advanced Settings”部分中添加附件时,要使用Jenkins环境变量${WORKSPACE}指定HTML测试报告的存储路径。
3.确认电子邮件发送功能已正确配置,包括SMTP服务器地址、端口、发件人、SMTP认证等。可以在Jenkins管理页面中的“系统设置”中进行相关配置。
希望这些步骤能帮助你完成将HTML测试报告作为附件通过Jenkins邮件发送的操作。
哥哥你可以在 Jenkins 的构建后操作中添加一个脚本来生成 HTML 测试报告并将其作为附件添加到电子邮件中。以下是一些示例代码:
// 生成测试报告
sh 'pytest --report=musen.html --title=柠檬班上课报告 --tester=测试员 --desc=报告描述信息 --template=2'
// 将测试报告添加为附件
def reportPath = "${WORKSPACE}/musen.html"
def reportName = "Test Report.html"
def attachment = new hudson.tasks.MailAttachment(reportPath, reportName)
currentBuild.attachments[attachment]
在这个例子中,首先使用 sh
命令(或者对应的 Windows 平台命令)运行 pytest 测试,并将结果保存到指定的文件路径。然后,创建一个 MailAttachment
对象,该对象包含要添加的文件路径和名称。最后,使用 currentBuild.attachments
将附件添加到当前构建中。
然后将这段脚本添加到 Jenkins 的“构建后操作”中,并确保已正确配置邮件通知以及其他相关参数,例如 SMTP 服务器、收件人等等!
要将自动生成的 pytest 测试报告作为附件发送到 Jenkins 邮件中,您需要在 Jenkins 构建流程中执行以下步骤:
安装 Jenkins 的 Email Extension 插件。该插件使您可以使用更多的邮件选项,包括添加附件。
使用 pytest-testreport
库生成 HTML 测试报告,可以使用以下命令:
pytest.main(['--html=report.html'])
上述代码将生成一个名为 report.html
的文件,其中包含测试结果。
对于您的 Jenkins 构建任务,请按照以下步骤操作:
<p>The test results are attached as an HTML file.</p>
<j:jelly xmlns:j="jelly:core">
<j:set var="attachments" value="">
<j:forEach var="file" items="${build.getWorkspace().list('**/report.html')}">
<j:set var="attachments">${attachments}<hudson.tasks.MailMessage$Attachment><jira:attachment src="${file}" contentType="text/html"/><name>report.html</name></hudson.tasks.MailMessage$Attachment></j:set>
</j:forEach>
${attachments}
</j:set>
<j:set var="body"><j:include page="$$DEFAULT_CONTENT$$"/></j:set>
<j:set var="contenttype">${contentType}</j:set>
</j:jelly>
这个代码段会查找工作空间中的所有名为 report.html
的文件,并将其作为附件添加到电子邮件中。
<h1>Test Results from Jenkins</h1>
现在您已经完成了 Jenkins 邮件设置的配置。您可以保存并运行该构建任务,Jenkins 将会通过邮件将 HTML 测试报告附件发送给所选的收件人。
第一步:在pytest.main()添加参数:--junitxml=xxxxx.html 指定html文件的路径
第二步:在jenkins的job任务的【构建后操作】中,添加【Publish Junit test result report】,配置测试结果html文件
第三步:编写一个html报告模板,并与自动化项目代码放在一起。html文件中,不能使用外部css样式文件,否则邮件正文中无法解析出样式 。
第四步:在jenkins的job任务的【构建后操作】中,添加【Editable Email Notification】,使用html文件。
在【Default Content】当中,使用以下表达式:
${FILE,path="test_templates.html"}
path=相对路径 相对于当前项目的工作空间
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
要实现将pytest-testreport生成的HTML测试报告作为附件添加到Jenkins邮件中,您可以使用Jenkins的email-ext插件进行配置。以下是需要进行的步骤:
步骤1:安装Jenkins的email-ext插件
进入Jenkins的插件管理页面,在搜索框中输入“email-ext”,在插件列表中找到该插件并安装。
步骤2:配置Jenkins的SMTP服务器信息
进入Jenkins的全局设置页面,在“E-mail Notification”中填写SMTP服务器信息、发件人信息等。
步骤3:配置邮件模板
在邮件模板中添加以下代码,用于添加pytest-testreport生成的HTML测试报告:
${FILE, path="path/to/your/reports/*.html", title="Test report", inline=false}
其中,path="path/to/your/reports/*.html"
需要替换成pytest-testreport生成的HTML测试报告文件所在的路径,title
为邮件中的标题,可以根据需要进行修改。
步骤4:在Jenkins的构建脚本中添加pytest-testreport命令
在Jenkins的构建脚本中添加您提供的pytest-testreport命令,用于生成HTML测试报告,例如:
pytest.main(['--report=musen.html',
'--title=柠檬班上课报告',
'--tester=测试员',
'--desc=报告描述信息',
'--template=2'])
请注意,这里的生成路径必须与邮件模板中的路径一致。
步骤5:配置Jenkins的邮件通知
在Jenkins的构建配置页面中,勾选“E-mail Notification”,并填写收件人信息等。
以上步骤完成后,Jenkins将在构建完成后自动发送包含HTML测试报告附件的邮件。
附:完整邮件模板示例代码
```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://w/
您好!您想在原有的基础上使用pytest-testreport生成HTML测试报告并通过Jenkins邮件发送出去。
首先,您可以指定pytest-testreport生成HTML测试报告的位置,例如:
pytest.main(['--report=musen.html', '--title=柠檬班上课报告', '--tester=测试员', '--desc=报告描述信息', '--template=2'])
在这里,--report参数指定了测试报告的名称为musen.html,测试报告将在当前目录下生成。如果您想将其放在特定的文件夹中,可以在报告名称前添加文件夹路径,例如--report=./reports/musen.html,这将在当前目录下的reports文件夹中生成测试报告。
接下来,您可以使用Jenkins的邮件插件来发送HTML测试报告。您可以在Jenkins中设置一个新的邮件模板,该模板将包含HTML测试报告作为附件。以下是一个Groovy脚本的例子,该脚本可以发送HTML测试报告:
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// 发送邮件的参数设置
def server = "smtp.example.com"
def port = "587"
def username = "your_username"
def password = "your_password"
def sender = "sender@example.com"
def recipient = "recipient@example.com"
def subject = "Test Report"
def body = "Please find the attached test report."
// 附件设置
def attachmentPath = "./reports/musen.html"
def attachmentName = "musen.html"
// 构建邮件
def props = new Properties()
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.starttls.enable", "true")
props.put("mail.smtp.host", server)
props.put("mail.smtp.port", port)
def session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password)
}
})
def message = new MimeMessage(session)
message.setFrom(new InternetAddress(sender))
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient))
message.setSubject(subject)
def multipart = new MimeMultipart()
// 添加正文
def bodyPart = new MimeBodyPart()
bodyPart.setText(body)
multipart.addBodyPart(bodyPart)
// 添加附件
def attachmentPart = new MimeBodyPart()
attachmentPart.attachFile(new File(attachmentPath))
attachmentPart.setFileName(attachmentName)
multipart.addBodyPart(attachmentPart)
// 发送邮件
message.setContent(multipart)
Transport.send(message)
在上面的示例中,您需要将server、port、username、password、sender、recipient和subject更改为您自己的值。attachmentPath和attachmentName变量应该包含您生成的HTML测试报告的路径和名称。
然后,您可以使用Jenkins的构建后操作来调用此脚本。在Jenkins的构建配置中,选择“构建后操作”选项卡,然后选择“发送邮件”选项。在“附件”部分中,添加生成的HTML测试报告作为附件。
希望这可以帮助到您!如果您