UnitTest+HTMLTestRunner实战
框架
步骤
先建立images,reports,tools文件夹
把HTMLTestRunner.py放进tools中 文件内容 https://www.cnblogs.com/wangxue1314/p/13493267.html
建立testcase文件,内容如下
import unittest from selenium import webdriver import time class IwebshopLoginTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() #self.driver.maximize_window() self.driver.implicitly_wait(10) self.driver.get(r"http://127.0.0.1/mgr/sign.html") time.sleep(1) def tearDown(self): self.driver.quit() def test_login(self): self.driver.find_element_by_id('username').send_keys('byhy') self.driver.find_element_by_id('password').send_keys('88888888') self.driver.find_element_by_tag_name('button').click() time.sleep(1) name = self.driver.find_element_by_class_name('info').text try: self.assertIn("管理员", name) except AssertionError as e: print('登陆失败') # png无损压缩,jpg有损压缩 self.driver.get_screenshot_as_file("./image/%s.pog" % str(time.time()).replace(".", "-")) raise def test_login_error(self): self.driver.find_element_by_id('username').send_keys('byhy') self.driver.find_element_by_id('password').send_keys('88888888') self.driver.find_element_by_tag_name('button').click() time.sleep(1) name = self.driver.find_element_by_class_name('info').text try: self.assertIn("管理员1", name) except AssertionError as e: print('内容不匹配') # png无损压缩,jpg有损压缩,错误截图 self.driver.get_screenshot_as_file("./images/%s.pog" % str(time.time()).replace(".", "-")) raise
建立login_test_run.py文件进行测试
import unittest from iwebshop_login_testcase import IwebshopLoginTest from tools.HTMLTestRunner import HTMLTestRunner suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(IwebshopLoginTest)) with open("./reports/iwebshop_login_test_report.html","wb") as f: h=HTMLTestRunner(stream=f,title="byhy登录模块自动化测试报告",description="windows10 chrome") h.run(suite)
运行结果
测试报告
错误截图