python+selenium+unittest 搭建web自动化测试框架(一)
1、总入口文件 first_ddt_case.py
import os import sys sys.path.append(os.getcwd()) import ddt import time import unittest import HTMLTestRunner from selenium import webdriver from business.register_business import RegisterBusiness from log.user_log import UserLog from base.configEmail import SendEmail from util.excel_util import ExcelUtil ex = ExcelUtil() data = ex.get_data() #ddt 数据驱动模块 ,支持多组参数循环传入 @ddt.ddt class FirstDdtCase(unittest.TestCase): @classmethod def setUpClass(cls): cls.log = UserLog() cls.logger = cls.log.get_log() cls.driver = webdriver.Chrome() cls.driver.get('http://www.5itest.cn/register') cls.driver.maximize_window() cls.login = RegisterBusiness(cls.driver) def setUp(self): self.driver.refresh() def tearDown(self): time.sleep(2) for method_name,error in self._outcome.errors: if error: case_name = self._testMethodName file_path = os.path.join(os.getcwd()+"/report/"+case_name+".png") self.driver.save_screenshot(file_path) @classmethod def tearDownClass(cls): cls.log.close_handle() cls.driver.close() @ddt.data(*data) def test_register_case(self,data): email,username,password,code,assertText = data email_error = self.login.register_function(email,username,password,code,assertText) self.assertTrue(email_error) if __name__ == '__main__': file_path = os.path.join(os.getcwd()+'/report/'+'first_case1.html') f = open(file_path,'wb') suite = unittest.TestLoader().loadTestsFromTestCase(FirstDdtCase) runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="This is first report1",description=u"这个是我们第一次测试报告1",verbosity=2) runner.run(suite) f.close() send_mail = SendEmail( username='xxxxxxx@qq.com', passwd='rrazxbbaldbgbj', recv=['vvvvvvvv@163.com'], title='接口自动化测试', content='测试发送邮件', file='d:/test_project/report/first_case1.html', ssl=True ) send_mail.send_email()
我的工程目录: