1、下载HTTPTestRunner
http://tungwaiyip.info/software/HTMLTestRunner.html
2、复制HTMLTestRunner.py至python安装目录的lib目录下,注意如果是用的anaconda中的python解释器,找到anaconda目录下的python解释器对应目录下
3、修改 HTMLTestRunner–>适应python3
# 第 94 行import StringIO修改为:import io#第 539 行self.outputBuffer = StringIO.StringIO()修改为:self.outputBuffer = io.StringIO()第 631 行print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改为:print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))#第 642 行if not rmap.has_key(cls):修改为:if not cls in rmap: 第 766 行uo = o.decode('latin-1')修改为:uo = e 第 772 行ue = e.decode('latin-1')修改为:ue = e
4、代码
test_baidu.py
from selenium import webdriverimport unittestimport timeclass MyTest(unittest.TestCase):"""阿彪的百度测试报告"""def setUp(self) -> None:self.driver = webdriver.Chrome(r'F:\QAGoGo\browserdriver\chromedriver_win32\chromedriver.exe')self.driver.maximize_window()self.driver.implicitly_wait(10)self.base_url = ""def test_baidu(self):""" 百度搜索unittest """driver = self.driverdriver.get(self.base_url + "/")driver.find_element_by_id("kw").clear()driver.find_element_by_id("kw").send_keys("unittest")driver.find_element_by_id("su").click()time.sleep(3)def tearDown(self) -> None:self.driver.quit()if __name__ == "__main__":unittest.main()
test_youdao.py
from selenium import webdriverimport unittestimport timefrom mon.by import Byclass MyTest(unittest.TestCase):""" 阿彪的有道测试报告 """def setUp(self) -> None:self.driver = webdriver.Chrome(r'F:\QAGoGo\browserdriver\chromedriver_win32\chromedriver.exe')self.driver.maximize_window()self.driver.implicitly_wait(10)self.base_url = ""def test_youdao(self):""" 有道搜索webdriver """driver = self.driverdriver.get(self.base_url + "/")driver.find_element_by_css_selector("#autosuggest input").send_keys("webdriver")driver.find_element(By.CSS_SELECTOR, "a.translate_btn").click()time.sleep(2)title = driver.titleprint(title)self.assertEqual(title, "webdriver")def tearDown(self) -> None:self.driver.quit()if __name__ == "__main__":unittest.main()
runtest.py
import unittest, timefrom HTMLTestRunner import HTMLTestRunnertest_dir = "./test_case"discover = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py")if __name__ == "__main__":now = time.strftime("%Y-%m-%d %H-%M-%S")file = './report/' + now + ' result.html'fp = open(file, 'wb')runner = HTMLTestRunner(stream=fp, title="测试报告", description=" 用例执行情况:")runner.run(discover)fp.close()
5、整合邮件发送
import unittest, time, os, smtplibfrom HTMLTestRunner import HTMLTestRunnerfrom email.header import Headerfrom email.mime.text import MIMEText# ==============定义发送邮件==================def send_email(file_new):f = open(file_new, 'rb')mail_body = f.read()f.close()msg = MIMEText(mail_body, 'html', 'utf-8')msg['Subject'] = Header("自动化测试报告", 'utf-8')smtp = smtplib.SMTP()smtp.connect("")smtp.login("ty45b_15034553545@", "KYIXTKSQSQSPBKIIOPPALXMSISJQO")smtp.sendmail("ty45b_15034553545@", "tefeyb_17836545444915@", msg.as_string())smtp.quit()print("email send successfully")# ======查找测试报告目录,找到最新生成的测试报告文件====def latest_report(reportDir):lists = os.listdir(reportDir)lists.sort(key=lambda fn: os.path.getmtime(reportDir + "\\" + fn))print("最新的文件为:" + lists[-1])return os.path.join(reportDir, lists[-1])if __name__ == "__main__":test_dir = "./test_case"test_report = "./report"discover = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py")now = time.strftime("%Y-%m-%d %H-%M-%S")file = './report/' + now + ' result.html'fp = open(file, 'wb')runner = HTMLTestRunner(stream=fp, title="测试报告", description=" 用例执行情况:")runner.run(discover)fp.close()new_report = latest_report(test_report)send_email(new_report)
设计模式
Page Object设计模式
如果觉得《自动化测试应用---HTML测试报告+邮件发送》对你有帮助,请点赞、收藏,并留下你的观点哦!