This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Hello ~ I’m Milo! I am building an open source interface testing platform from 0 to 1, and I am also writing a complete tutorial corresponding to it. I hope you can support it. Welcome to pay attention to my public number test development pit goods, get the latest article tutorial!

review

Back to the book, we found an email template that looked good, but since the data was dead, we needed to get the data from the test report and render the HTML template.

In this video, we’re going to get to the bottom of that.

rendering

This time the data has been real, and there is a regular task directly run sent out, quite reliable!

Compose email module

Since yagmail wasn’t very useful before, we need to rewrite the send_msg method, so I’ll just go to the code because it’s easier.

To do this, we need to add a field to the configuration file:

Smtplib actually offers the From option. I was going to call it Pity, but then my email wouldn’t go anywhere and I gave it up.

    @staticmethod
    def send_msg(subject, content, attachment=None, *receiver) :
        configuration = SystemConfiguration.get_config()
        data = configuration.get("email")
        sender = data.get("sender")
        to = data.get("to")
        message = MIMEText(content, 'html'.'utf-8')
        message['From'] = sender
        message['To'] = Header(to, 'utf-8')
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtp = smtplib.SMTP()
            smtp.connect(data.get("host"))
            Set_debuglevel (1) prints all information about the SMTP server.
            smtp.set_debuglevel(1)
            smtp.login(sender, data.get("password"))
            smtp.sendmail(sender, list(receiver), message.as_string())
        except Exception as e:
            raise Exception(F "Failed to send test report email:{e}")
Copy the code

The new send_msg method is simple, with much more code than yagmail while keeping the parameters consistent.

The idea is to encapsulate MIMETEXT, set it to HTML mode, put the recipient, sender, subject, and content data into it, and send the message via SendMail.

Modify the run_test_plan method

  • Add executor parameters

At present, manual execution is not supported in our test plan. In order to adapt to manual execution and reflect the executor information in the email, we add the executor parameter in run_test_plan:

The default executor is 0, which is CPU.

  • Rewrite the run_multiple method

    The run_multiple method is the core method for our test plan execution, because our test plan supports multiple environments and each test environment corresponds to one test report.

    After we execute a test plan, there may be multiple report links, and each environment has its own pass rate.

    So we need to record a map of env => test results that will return.

    Since we are executing asynchronously, we set up a map externally and pass it in as a parameter. Due to the principle of passing references, the function completes and our map is updated.

Naturally, the run_multiple method also needs to be modified. It should accept the new map parameters, but it can be passed without affecting the original function.

Here’s how to get the name of the executor:

Then the run_multiple changes before returning:

Generating real HTML

All of this is to get the data needed for the test report, and once we get it, we have to render the HTML using Jinja2.

Here I put native HTML:

<! DOCTYPEhtml>
<html>
<head>
	<title>The test report</title>
</head>
<body>
<div>
    <includetail>
        <div align="center">
            <div class="open_email" style="margin-left: 8px; margin-top: 8px; margin-bottom: 8px; margin-right: 8px;">
                <div>
                    <br>
                    <span class="genEmailContent">
                        <div id="cTMail-Wrap"
                             style="word-break: break-all; box-sizing:border-box; text-align:center; min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:'helvetica neue',PingFangSC-Light,arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif">
                            <div class="main-content" style="">
                                <table style="width:100%; font-weight:300; margin-bottom:10px; border-collapse:collapse">
                                    <tbody>
                                    <tr style="font-weight:300">
                                        <td style="width:3%; max-width:30px;"></td>
                                        <td style="max-width:600px;">
                                            <div id="cTMail-logo" style="width:92px; height:25px;">
                                                <a href="">
                                                    <img border="0" src="https://gitee.com/woodywrx/picture/raw/master/2021-11-24/1637761462006-image.png"
                                                         style="width:36px; height:36px; display:block">
                                                </a>
                                            </div>
                                            <p style="height:2px; background-color: #00a4ff; border: 0; font-size:0; padding:0; width:100%; margin-top:20px;"></p>
                                            <div id="cTMail-inner" style="background-color:#fff; padding:23px 0 20px; Box-shadow: 0px 1px 1px 0px Rgba (122, 55, 55, 0.2); text-align:left;">
                                                <table style="width:100%; font-weight:300; margin-bottom:10px; border-collapse:collapse; text-align:left;">
                                                    <tbody>
                                                    <tr style="font-weight:300">
                                                        <td style="Width: 3.2%; max-width:30px;"></td>
                                                        <td style="max-width:480px; text-align:left;">
                                                            <h1 id="cTMail-title" style="font-size: 20px; line-height: 36px; margin: 0px 0px 22px;"><strong>{{ env }}</strong>Test Plan [<strong>{{ plan_name }}</strong>【 Execution results:<strong>{{plan_result}} </strong>
                                                            </h1>

                                                            <p id="cTMail-userName" style="font-size:14px; color:#333; line-height:24px; margin:0;">Dear pity users, hello!</p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">Use cases are executed in this test plan<strong>{{ total }}</strong>The article.</span>
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;"><strong>{{ executor }}</strong>When {{start_time}} starts, it takes {{cost}} seconds.</span>
                                                            </p>

                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">Number of successful<strong style="color: #67C23A">{{ success }}</strong>
                                                                </span>
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">Number of failures<strong style="color: #F56C6C">{{ failed }}</strong>
                                                                </span>
                                                            </p>
                                                           <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">Wrong number<strong style="color: #E6A23C">{{ error }}</strong>
                                                                </span>
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">Skip the number<strong style="color: #409EFF">{{ skip }}</strong>
                                                                </span>
                                                            </p>

                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">For a detailed test report, click the link below.<span style="font-weight: bold;">Please understand if there is any disturbance.</span>
                                                                </span>
                                                            </p>

                                                            <p class="cTMail-content"
                                                               style="font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;">
                                                                <a id="cTMail-btn" href="{{report_url}}" title=""
                                                                   style="font-size: 16px; line-height: 45px; display: block; background-color: rgb(0, 164, 255); color: rgb(255, 255, 255); text-align: center; text-decoration: none; margin-top: 20px; border-radius: 3px;">Click here for the full report</a>
                                                            </p>

                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    <br>Can't display properly? Please copy the following link to open your browser:<br>
                                                                    <a href="{{report_url}}" title=""
                                                                       style="color: rgb(0, 164, 255); text-decoration: none; word-break: break-all; overflow-wrap: normal; font-size: 14px;">Here is the link to the test report</a>
                                                                </span>
                                                            </p>
                                                        </td>
                                                        <td style="Width: 3.2%; max-width:30px;"></td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </div>

                                            <div id="cTMail-copy" style="text-align:center; font-size:12px; line-height:18px; color:#999">
                                                <table style="width:100%; font-weight:300; margin-bottom:10px; border-collapse:collapse">
                                                    <tbody>
                                                    <tr style="font-weight:300">
                                                        <td style="Width: 3.2%; max-width:30px;"></td>
                                                        <td style="max-width:540px;">

                                                            <p style="text-align:center; margin:20px auto 14px auto; font-size:12px; color:#999;">Please do not reply to this email sent automatically by pity.</p>

                                                            <p id="cTMail-rights" style="max-width: 100%; margin:auto; font-size:12px; color:#999; text-align:center; line-height:22px;">
                                                                <img border="0" src="https://gitee.com/woodywrx/picture/raw/master/2021-8-7/1628267097936-qrcode_for_gh_554fe7a74955_258.jpg"
                                                                     style="width:84px; height:84px; margin:0 auto;">
                                                                <br>Check out the test development pit and learn more about Pity<br>
                                                            </p>
                                                        </td>
                                                        <td style="Width: 3.2%; max-width:30px;"></td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                        <td style="width:3%; max-width:30px;"></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </span>
                </div>
            </div>
        </div>
    </includetail>
</div>

</body>
</html>
Copy the code

The above code in pity/templates/report. The HTML file.

Then write a method to render HTML:

Where kwargs passes the corresponding parameters to HTML.

As mentioned earlier, one email corresponds to one address, so the location of the send_msg call will have to change as well.

At this point, our email push function is complete.

That’s all for today and I’ll see you next time.