Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在Robot框架中使用Python发送测试结果的电子邮件通知_Python 2.7_Robotframework - Fatal编程技术网

Python 2.7 在Robot框架中使用Python发送测试结果的电子邮件通知

Python 2.7 在Robot框架中使用Python发送测试结果的电子邮件通知,python-2.7,robotframework,Python 2.7,Robotframework,我们使用ROBOT框架来执行自动化测试用例。 谁能指导我写测试结果电子邮件通知的脚本 注: 我有电子邮件服务器的详细信息 问候,, -kranti我使用smtplib和MIMEText import smtplib from email.mime.text import MIMEText class EmailClient(): def __init__(self, my_address): self.my_address = my_address def

我们使用ROBOT框架来执行自动化测试用例。 谁能指导我写测试结果电子邮件通知的脚本

注: 我有电子邮件服务器的详细信息

问候,,
-kranti

我使用smtplib和MIMEText

import smtplib
from email.mime.text import MIMEText

class EmailClient():

    def __init__(self, my_address):
        self.my_address = my_address

    def send(self, message, subject, user, email):

        header = "Hello " + str(user) + ",\n\n"
        footer = "\n\n-Your Boss"
        msg = MIMEText(header + message + footer)

        msg['Subject'] = subject
        msg['From'] = self.my_address
        msg['To'] = email

        s = smtplib.SMTP('localhost')
        s.sendmail(self.my_address, [email], msg.as_string())
        s.quit()

EClient = EmailClient("MyEmail@University.edu")
EClient.send("This is a test Email", "Test Subject", "John Doe", "jdoe@University.edu")

您可以使用jenkins运行Robot框架测试用例。jenkins中有一个自动生成邮件选项,用于发送带有测试结果的邮件。

您可以为发送电子邮件创建自定义库

详情请参阅

我做了类似的事情,我基于此创建了库

python文件中的函数示例:

import smtplib
from io import StringIO
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText

from email import encoders
import os


def send_mail_no_attachment(server, from_user, from_password, to, subject, text):
    msg = MIMEMultipart()

    msg['From'] = from_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    mailServer = smtplib.SMTP(server)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(from_user, from_password)
    mailServer.sendmail(from_user, to, msg.as_string())
    mailServer.close()
调用robot文件中的函数:

*** Test Cases ***
example mail
    send mail no attachment  ${SMTP_SERVER}  ${USER}  ${PASS}  ${mail}  ${subject}  ${text}

如果您对robot框架不是很精通,请只定义函数,不要定义类,您可以在算法中将此函数作为关键字调用。

我有类似的要求,因此使用python文件我已经达到了要求(需要在robot执行后执行.py文件)

项目和

测试执行后如何发送电子邮件

  • 将robotemail.py复制到文件
  • 创建bat文件(按顺序执行robot命令和.py文件)

    机器人测试&& python robotemail.py

  • 执行bat文件

  • 电子邮件将发送给收件人

  • 非常感谢您的回复。您能告诉我如何使用此代码从机器人框架导入测试结果吗。