这里我使用的是python selenium webdriver环境,浏览器驱动安装见selenium安装
1、下载HTMLTestRunner.py:http://tungwaiyip.info/software/HTMLTestRunner.html
python3环境需要对该文件的六个地方进行修改,修改后放置pythonLib目录下
import unittest,time,os from selenium import webdriver from config.HTMLTestRunner_Chart_Change import HTMLTestRunner from config.Dir_Path import BASE_PATH class BaiduTest(unittest.TestCase): def setUp(self): #测试准备工作 self.driver = webdriver.Chrome() #加载驱动 self.driver.implicitly_wait(30) #隐式等待 self.base_url = "http://www.baidu.com" #url def test_baidu(self): #测试用例 self.driver.get(self.base_url + "/") #get发送url self.driver.find_element_by_id("kw").clear() #清空 self.driver.find_element_by_id("kw").send_keys("python") #搜索shuzf self.driver.find_element_by_id("su").click() #点击 time.sleep(2)#休息5s if not os.path.exists('report/image/'): os.makedirs('report/image/') # 判断当前路径是否存在,没有则创建文件夹 pic_path = 'report/image/' + time.strftime("%Y-%m-%d-%H_%M_%S") + '.png' self.driver.save_screenshot(pic_path) # 生成图片 print(os.path.join(BASE_PATH,pic_path)) self.assertEqual(self.driver.title,"python_百度搜索",) # 断言判断是否相等 def tearDown(self): #释放资源 self.driver.quit() #退出 if __name__ == "__main__": suite= unittest.TestSuite() #构造测试套件 suite.addTest(BaiduTest("test_baidu")) #添加测试用例 file_name = "report/html/" + time.strftime("%Y-%m-%d-%H_%M_%S")+ ".html" with open(file_name, 'wb') as f: HTMLTestRunner(stream=f, title='API Test Report', description='Test details').run(suite) # 执行套件 f.close()
2,使用命令行执行文件,而不是pycharm
>python demo.py