博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 通过配置文件 读取参数,执行测试用例,生成测试报告并发送邮件
阅读量:5059 次
发布时间:2019-06-12

本文共 5374 字,大约阅读时间需要 17 分钟。

 

 

 

#-*-coding:utf-8-*-#测试用例配置参数#XXXXX_Uitest->baseinfo->__init__.pybase_url = "http://XXXXxxxxx.com"undirect_username = "XXXX"undirect_password = "XXXXX"direct_username = XXXXX"direct_password = "xxxxxx"#发送邮件配置参数Smtp_Server = 'smtp.mxhichina.com'Smtp_Sender = 'XXXXX@xxx。com'Smtp_Sender_Password = 'XXXXXXX'Smtp_Receiver = ['XXXXXX@123.cn','SSSSSS@123.cn']#测试用例及报告路径配置参数test_dir = './test_case'test_report = './test_report/'

 

 

#测试用例,测试代码 #test_cart.py import unittestimport osimport timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysclass TestCart(unittest.TestCase):    def setUp(self):        print('setup')        self.url = 'http://www.XXX.mm'        self.driver = webdriver.Firefox()        self.driver.implicitly_wait(5)        self.driver.get(self.url)        self.driver.maximize_window()        print("open browser")        #return self.driver    def get_Info(self):        filePath = os.path.join(os.getcwd(),'user_info')        filename = filePath + '\\user_info.txt'        print(filename)        fo = open(filename, 'r')        lines = fo.readlines()        for data in lines:            name,pwd = data.split(',')        print(name, pwd)        return name,pwd    def test_to_login(self):        '''验证是否登陆'''        name,pwd = self.get_Info()        time.sleep(5)        print(name,pwd)        self.driver.implicitly_wait(5)        self.driver.find_element_by_id('new-username').clear()        self.driver.find_element_by_id('new-username').send_keys(name)        self.driver.find_element_by_id('new-password').clear()        self.driver.find_element_by_id('new-password').send_keys(pwd)        self.driver.implicitly_wait(5)        #回车登陆,因为登陆按钮不好定位        self.driver.find_element_by_xpath("html/body/div[9]/div[3]/div[1]/div[1]/div[4]/span").send_keys(Keys.ENTER)        self.driver.implicitly_wait(10)        login_test = self.driver.find_element_by_xpath(".//*[@id='loginOut']").text        self.assertEqual(login_test, '[退出]')    def tearDown(self):        print("tearDown")        time.sleep(5)        print("close browser")        self.driver.quit()if __name__ == '__main__':    unittest.main()

 

 

 

 

#runtest.pyimport smtplibimport unittestimport timeimport xlrdimport osfrom HTMLTestRunner import HTMLTestRunnerfrom email.header import Headerfrom email.mime.text import MIMEText #导入baseinfo import baseinfofrom email.mime.multipart import MIMEMultipartdef get_sender():    filePath =  os.path.join(os.getcwd(),'user_info')    print("get_sender")    print(filePath)    fileName = filePath + '\Mail_info.xlsx'    print(fileName)    readExcel = xlrd.open_workbook(fileName)    sender = readExcel.sheet_by_name('sender')    get_sender = sender.row_values(1)    #print(get_sender)    return get_senderdef get_Receiver():    filePath =  os.path.join(os.getcwd(),'user_info')    fileName = filePath + '\Mail_info.xlsx'    print("get_Receiver()")    print(fileName)    readExcel = xlrd.open_workbook(fileName)    receiver_sheet = readExcel.sheet_by_name('receiver')    receiver_temp = receiver_sheet.col_values(0)    getReceiver = receiver_temp[1:]    print(getReceiver)    print(type(getReceiver))    return getReceiverdef get_NewReport(testreport):    dirs = os.listdir(testreport)    dirs.sort()    newreportname = dirs[-1]    print('The new report name: {0}'.format(newreportname))    file_new = os.path.join(testreport, newreportname)    print(file_new)    return file_newdef send_Mail(file_new):    f = open(file_new, 'rb')    # 读取测试报告正文    mail_body = f.read()    f.close()    try:     #通过 baseinfo 引用 __init__ 初始化的参数值        smtp = smtplib.SMTP(baseinfo.Smtp_Server, 25)        sender = baseinfo.Smtp_Sender        password = baseinfo.Smtp_Sender_Password        receiver = baseinfo.Smtp_Receiver        smtp.login(sender, password)        msg = MIMEMultipart()        # 编写html类型的邮件正文,MIMEtext()用于定义邮件正文        # 发送正文        text = MIMEText(mail_body, 'html', 'utf-8')        text['Subject'] = Header('自动化测试报告', 'utf-8')        msg.attach(text)        # 发送附件        # Header()用于定义邮件标题        msg['Subject'] = Header('自动化测试报告', 'utf-8')        msg_file = MIMEText(mail_body, 'html', 'utf-8')        msg_file['Content-Type'] = 'application/octet-stream'        msg_file["Content-Disposition"] = 'attachment; filename="TestReport.html"'        msg.attach(msg_file)        #定义发件人,如果不写,发件人为空        msg['From'] = sender        #定义收件人,如果不写,收件人为空        msg['To'] = ",".join(receiver)        tmp = smtp.sendmail(sender, receiver, msg.as_string())        print(receiver)        print(tmp)        smtp.quit()        return True    except smtplib.SMTPException as e:        print(str(e))        return Falseif __name__ == '__main__':    test_dir = os.path.join(os.getcwd(),'test_case')    print(test_dir)    report_dir = os.path.join(os.getcwd(),'report')    test_discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')    print("test_discover")    now = time.strftime("%Y-%m-%d-%H_%M_%S")    filename = report_dir+'/result-'+now+'.html'    fp = open(filename, 'wb')    runner = HTMLTestRunner(stream = fp, title = 'UI自动化测试报告', description = '用例执行情况')    #runner = unittest.TextTestRunner()    runner.run(test_discover)    fp.close()    new_report = get_NewReport(report_dir)    mail = send_Mail(new_report)    if mail:        print("发送成功!")    else:        print("发送失败!")

 

转载于:https://www.cnblogs.com/Skyyj/p/6763529.html

你可能感兴趣的文章
防止你的iPhone程序遭盗版(入门篇)
查看>>
cocos2d查看OpenGL当前所绑定的纹理
查看>>
详解用em替换px
查看>>
win10家庭版启用远程桌面
查看>>
用于所有Windows驱动程序开发者的概念
查看>>
zabbix基本功能操作
查看>>
Bootstrap+JSP实例学习笔记一.简单的带登录功能的首页
查看>>
浅谈C#与数据结构中的哈希表(Hashtable)(上)(没法转载,只能贴在这里啦)
查看>>
Git 的使用
查看>>
原码 反码 补码 移码
查看>>
JS 二维数组 对象数组 对象中的数组
查看>>
转:前端工程与性能优化(下):静态资源管理与模板框架
查看>>
转:Hprose for php(二)——服务器
查看>>
extern static和函数
查看>>
HBase环境搭建
查看>>
拍照、本地图片工具类(兼容至Android7.0)
查看>>
NGUI-使用UILabel呈现图片和不同格式的文字
查看>>
AutoTransformHandler
查看>>
angular5引入sass
查看>>
Gridview 动态指定字段升序,降序排序
查看>>