首页 > 代码库 > Python 通过配置文件 读取参数进行
Python 通过配置文件 读取参数进行
#-*-coding:utf-8-*- #测试用例配置参数 #XXXXX_Uitest->baseinfo->__init__.py base_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 unittest import os import time from selenium import webdriver from selenium.webdriver.common.keys import Keys class 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.py import smtplib import unittest import time import xlrd import os from HTMLTestRunner import HTMLTestRunner from email.header import Header from email.mime.text import MIMEText
#导入baseinfo import baseinfo from email.mime.multipart import MIMEMultipart def 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_sender def 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 getReceiver def 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_new def 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 False if __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("发送失败!")
Python 通过配置文件 读取参数进行
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。