* 项目目标
对百度登录页面(https://passport.baidu.com/v2/?login)进行登录测试
* 功能实现
- 自动运行用例
- 自动生成测试报告
- 自动断言与测试截图
- 自动将最新测试报告发送到指定邮箱
- PageObject + Unittest + 数据驱动
* 工程目录
- base:基础共享包,通用定位元素方式封装
- config:配置文件、配置信息存放的地方
- util:存放第三方模块,发送邮件、读取报告等数据操作
- page_obj:实现登录时,目标元素定位、自动发送信息等
- business:执行page_obj包中相关py文件,逻辑操作等
- case:内置程序main文件,真正case用例
- report:测试报告 + 测试error截图
* 各模块实现
1、config模块
1)配置文件:LocalElement.ini
[LoginElement] user_name=id>TANGRAM__PSP_3__userName password=id>TANGRAM__PSP_3__password login_error=id>TANGRAM__PSP_3__error login_submit=id>TANGRAM__PSP_3__submit login_loginbtn=id>TANGRAM__PSP_3__footerULoginBtn
2)配置信息:setting.py
import os base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 项目首路径 # report_path = os.path.join(base_dir,'report','first_case.html') # 测试报告路径 config_ini = os.path.join(base_dir,'config','localElement.ini') # localElement.ini 配置文件路径 report_path = os.path.join(base_dir,'report') # report 配置文件路径
2、util模块
1)read_ini.py:读取配置文件内容
from config.setting import config_ini import configparser class Read_Ini(object): """读取配置文件信息""" def __init__(self,node = None): if node: self.node = node else: self.node = 'LoginElement' # 配置文件中的某个节点 self.cf = self.load_ini() def load_ini(self): # 加载文件 cf = configparser.ConfigParser() # 使用 configparser模块读取配置文件信息 cf.read(config_ini) # 配置文件所在路径 return cf def get_value(self,key): # 获取配置文件中key的value值 data = self.cf.get(self.node,key) return data # if __name__ == '__main__': # read_init = Read_Ini() # print(read_init.get_value('user_name'))
2)get_last_report.py:获取最近创建的测试报告
import os from config import setting class GetLastReport(object): """获取最近一次测试报告""" def last_report_file(self): lists = os.listdir(setting.report_path) last_report_file = self.get_last_report_file(lists) # print(last_report_file) return last_report_file def get_last_report_file(self,lists): report_list = [] for l_list in lists: if l_list.endswith('html'): report_list.append(l_list) last_report_file = report_list[-1] # print(report_list) return last_report_file if __name__ == '__main__': get_last_report = GetLastReport() get_last_report.last_report_file()
3)send_email.py:测试完成后自动发送邮件 -->测试报告.html
import smtplib from email.mime.text import MIMEText from email.header import Header from util.get_last_report import GetLastReport from config import setting import os last_report = GetLastReport() report_file = last_report.last_report_file() class SendEmail: def __init__(self,receivers = None): self.mail_host = "smtp.163.com" # 设置服务器 self.mail_user = "13*******[email protected]" # 用户名 self.mail_pass = "密令" # 密令/密码 self.sender = '13*******[email protected]' if receivers: self.receivers = receivers else: self.receivers = ['[email protected]'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 def get_report_file(self,report_file): """获取最近一次测试报告""" report_path = os.path.join(setting.report_path,report_file) with open(report_path,'rb') as f: # f = open(report_path,'rb') mail_content = f.read() return mail_content def send_email(self,report_file): content = self.get_report_file(report_file) message = MIMEText(content, _subtype='html', _charset='utf-8') # 邮件内容 subject = '百度登录-自动化测试报告' message['Subject'] = Header(subject, 'utf-8') # 邮件主题 message['From'] = self.sender # 发送人,必填,邮箱格式 message['To'] = ";".join(self.receivers) # 收件人,必填,邮箱格式 server = smtplib.SMTP() server.connect(self.mail_host, 25) # 连接服务器 server.login(self.mail_user, self.mail_pass) # 登录 try: server.sendmail(self.sender, self.receivers, message.as_string()) print('发送成功') except smtplib.SMTPException as e: print("Error: 无法发送邮件") server.close() if __name__ == "__main__": send_email = SendEmail() send_email.send_email(report_file)
4)HTMLTestRunner.py:用于生成测试报告,HTML格式
""" A TestRunner for use with the Python unit testing framework. It generates a HTML report to show the result at a glance. The simplest way to use this is to invoke its main method. E.g. import unittest import HTMLTestRunner ... define your tests ... if __name__ == '__main__': HTMLTestRunner.main() For more customization options, instantiates a HTMLTestRunner object. HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g. # output to a file fp = file('my_report.html', 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='My unit test', description='This demonstrates the report output by HTMLTestRunner.' ) # Use an external stylesheet. # See the Template_mixin class for more customizable options runner.STYLESHEET_TMPL = '' # run the test runner.run(my_test_suite) ------------------------------------------------------------------------ Copyright (c) 2004-2007, Wai Yip Tung All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name Wai Yip Tung nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ # URL: http://tungwaiyip.info/software/HTMLTestRunner.html __author__ = "Wai Yip Tung" __version__ = "0.8.2" """ Change History Version 0.8.2 * Show output inline instead of popup window (Viorel Lupu). Version in 0.8.1 * Validated XHTML (Wolfgang Borgert). * Added description of test classes and test cases. Version in 0.8.0 * Define Template_mixin class for customization. * Workaround a IE 6 bug that it does not treat %(heading)s %(report)s %(ending)s