我是用python+pytest+allure+Jenkins跑的UI自动化用例,Jenkins是用的本地结果就是生成的allure测试报告通过Jenkins邮件发送后别人不能访问,现在想法是生成一个HTML的测试报告通过Jenkins邮件发送出去别人就可以在浏览器访问HTML的测试报告了,在Jenkins里用的是groovy的邮件模板,希望有人能帮助我这个newcomer指导一下Jenkins要怎么设置,代码怎么写尽量详细。
这是jenkins上cmd命令:
d:
cd D:\pythondir\pomoa\test_cases
call python -m pytest test_oacase.py --alluredir=C:\Users\58305\.jenkins\workspace\OA门户自动化拨测\allure-result --clean-alluredir
exit 0
这是我使用的groovy模板:
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和自己的思路:
首先,要生成一个HTML测试报告,可以使用pytest-cov插件。在安装pytest-cov插件之后,运行pytest --cov-report html命令就可以生成HTML测试报告了。
其次,要将HTML测试报告通过Jenkins邮件发送,可以使用Jenkins的邮件插件。在Jenkins的构建步骤中,添加一个“Editable Email Notification”构建后操作,将HTML测试报告作为附件添加到邮件中。
下面是一个示例的groovy邮件模板,可以将邮件中的附件作为链接显示在邮件正文中:
<html>
<body>
<p>Dear ${BUILD_USER_FIRST_NAME},</p>
<p>The test results are attached to this email. Please find the HTML report here:</p>
<p>${JENKINS_URL}/${JOB_NAME}/${BUILD_NUMBER}/coverage_html/index.html</p>
</body>
</html>
在模板中,${JENKINS_URL}/${JOB_NAME}/${BUILD_NUMBER}/coverage_html/index.html
是HTML测试报告的路径。将它添加到邮件正文中,就可以让收件人在浏览器中查看测试报告了。
另外,还可以将Jenkins的配置文件用Jenkins文件参数化插件进行参数化,配置HTML测试报告的路径和Jenkins的邮件参数。这样,就可以方便地在不同的Jenkins实例之间共享邮件配置了。
参考GPT和自己的思路:你可以通过以下步骤把UI自动化测试结果生成静态HTML测试报告并通过Jenkins邮件发送出去:
安装allure
你需要先安装allure工具来生成HTML测试报告。你可以在allure官网下载安装包并完成安装。
测试用例中添加allure装饰器
在测试用例中添加allure装饰器,建议采用pytest-allure-adaptor,这个装饰器可以自动生成对应的allure测试报告。
在Jenkins中添加allure插件
在Jenkins中添加allure插件,然后在Jenkins中配置构建任务时选择Generate Allure Report选项,来构建allure测试报告。
在Jenkins中添加邮件发送功能
在Jenkins中添加Jenkins Email Extension Plugin插件来添加邮件发送功能。在插件安装完成后,配置邮件发送信息,包括邮件主题、收件人、抄送等等信息。
在Jenkins中配置邮件模板
在Jenkins中可以配置邮件模板来美化邮件。你可以根据你的需求,按照你提供的groovy模板文件的格式,来编写模板文件。
生成静态HTML测试报告并发送邮件
在Jenkins完成构建任务后,会生成allure测试报告,在转换成静态HTML文件后通过插件发送邮件给收件人。
总结
以上是将UI自动化测试结果生成静态HTML测试报告并通过Jenkins邮件发送出去的步骤。其中,allure工具可以大大简化测试报告生成工作。通过Jenkins配置邮件模板,可以让测试报告更清晰美观。不过,在实际操作中可能会涉及到一些技术细节的问题,需要根据具体情况进行调整。