Jeevan.fu 2023-03-19 16:51 采纳率: 50%
浏览 75
问题最晚将于03月27日00:00点结题

怎么把UI自动化测试结果添加一个静态HTML测试报告作为附件通过Jenkins邮件发送出去

我现在是python写的代码通过Jenkins运行后生成的allure的测试报告,但是Jenkins是本地的通过邮件把allure测试报告的地址发送出去后是访问不了的,所以我现在想在原来基础上用pytest-testreport生成一个html的测试报告文件后在通过Jenkins邮件发送出去。

img

这是我准备使用再用例下生成html格式的测试报告命令,html报告存放的地方怎么指定在哪个文件夹:

import pytest

pytest.main(['--report=musen.html',
             '--title=柠檬班上课报告',
             '--tester=测试员',
             '--desc=报告描述信息',
             '--template=2'])

这个是我在Jenkins上使用的groovy邮件模板,能不能再这个模板上添加一个发送html文件测试报告的代码,让每次执行完用例把最新的html格式的测试报告通过邮件发送出去,还有Jenkins该怎么配置请详细指导一下我这个newcomer。

<!DOCTYPE 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>
 


  • 写回答

6条回答 默认 最新

  • 「已注销」 2023-03-19 17:15
    关注

    参考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函数。
    希望这可以帮助您。

    评论

报告相同问题?

问题事件

  • 已采纳回答 3月21日
  • 创建了问题 3月19日

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在