The test platform series (88) completes the email notification function (with a beautiful email template)

Posted by fangorn on Wed, 19 Jan 2022 16:39:54 +0100

Hello ~ I'm Milo!

I'm building an open source interface testing platform from 0 to 1, and I'm also writing a complete set of corresponding tutorials. I hope you can support me.

Welcome to my official account MENOIR's diary, get the latest article tutorial!

review

Last time, we found a seemingly good email template, but because the data is dead, we need to obtain the data generated by the test report and render the HTML template.

In this section, we will thoroughly improve this function.

design sketch

The data has been real this time, and there are regular tasks to send directly, which is quite reliable!

Write mail module

Since yagmail was not easy to use before, we need to rewrite send_msg method, because it is relatively simple, I will directly the code.

But before that, we need to add a field in the configuration file:

In fact, smtplib provides the From option. I originally wanted to call Pity automated test platform, but in that case, the email can't be sent out, so I gave 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"))
            # We use set_ Debug level (1) can print out all the information interacting with 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}")

New send_msg method is very simple. With the same parameters, the amount of code is much more than that of yagmail.

The general idea is to first encapsulate mime text and set it to html mode, then put the recipient, sender, subject, content and other data into it, and finally send mail through sendmail.

Transform run_test_plan method

  • Add executor parameter

At present, our test plan does not support manual execution. On the one hand, to adapt to manual execution and on the other hand, to reflect the executor's information in the e-mail, we run_test_plan plus executor:

The default value of the executor is 0, that is, CPU (the feeling when playing bully as a child)

  • Overwrite run_multiple method

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

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

    So we need to record a map, which stores the mapping of Env = > test results, and finally return.

    Because we execute asynchronously, we set a map externally and pass it in as a parameter. Due to the principle of reference passing, the function is executed and our map is updated.

Naturally, run_ The multiple method also needs to be modified. It should receive new map parameters, but it can be passed or not in order not to affect the original function.

The following is the operation to obtain the executor's name:

Then run_ Transformation of multiple before returning:

Generate real html

The above operations are to obtain the data required for the test report. After obtaining the data, we have to use jinja2 to render html.

Here I put the native html:

<!DOCTYPE html>
<html>
<head>
	<title>
		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>]results of enforcement: <strong>{{plan_result}} </strong>
                                                            </h1>

                                                            <p id="cTMail-userName" style="font-size:14px;color:#333; line-height:24px; margin:0;">
                                                                honorific pity Hello, user!
                                                            </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;">
                                                                    This test plan has a total of execution cases<strong>{{ total }}</strong>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;">
                                                                    from<strong>{{ executor }}</strong>to{{ start_time }}Start execution, total time{{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 successes <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;">
                                                                    Number of errors <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 quantity <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;">To view the detailed test report, click the link below.
                                                                    <span style="font-weight: bold;">If you have any trouble, please understand.</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 to view 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 normally? Please copy the following link to the browser to open:
                                                                    <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 test report link
                                                                    </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;">
                                                                This message was sent by pity Automatically sent, please do not reply.
                                                            </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>
                                                                Pay attention to test and development pit goods and understand pity More
                                                                <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>

The above code is in pit / templates / report HTML file.

Then write a method to render html:

Where kwargs is to pass the corresponding parameters to html.

As mentioned before, an email corresponds to an address, so send_ The place where MSG is called must also be modified.

At this point, our email push function has been completed.

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

Topics: Python React Testing FastAPI