#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/11/23 13:15 # @Author : zhouyang # @File : TestCase.py import unittest from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait class SearchTestCase(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get("https://www.baidu.com/") self.driver.implicitly_wait(15) def test_searchChina(self): """百度搜索中国的测试用例""" self.driver.find_element_by_xpath(".//*[@id=‘kw‘]").send_keys("中国") self.driver.find_element_by_xpath(".//*[@id=‘su‘]").click() WebDriverWait(self.driver, 15).until(lambda x: x.find_element_by_xpath(".//*[@id=‘1‘]/h3/a")) result = self.driver.find_element_by_xpath(".//*[@id=‘1‘]/h3/a").text self.assertEqual(result, "中国政府网_中央人民政府门户网站") def tearDown(self): self.driver.close() self.driver.quit() if __name__ == ‘__main__‘: unittest.main()
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/11/23 11:49 # @Author : zhouyang # @File : unittest_test2_3.py ‘‘‘把TestCase运行结果生成HTML格式的测试报告‘‘‘ import HTMLTestRunner import unittest from time import strftime, localtime, time from TestCase import SearchTestCase # 获取TestSuite的实例对象 suite = unittest.TestSuite() # 把测试用例添加到测试容器中 suite.addTest(SearchTestCase("test_searchChina")) ‘‘‘ #用TestLoader()的 loadTestsFromTestCase() 方法创建一个实例对象等同于TestSuite的实例对象 suite = unittest.TestLoader().loadTestsFromTestCase(SearchTestCase) ‘‘‘ # 获取当前时间 now = strftime("%Y-%m-%d-%H_%M_%S", localtime(time())) # 文件名 filename = now + "test.html" # 以二进制的方式打开文件并写入结果 fp = open(filename, "wb") runner = HTMLTestRunner.HTMLTestRunner( stream=fp, verbosity=2, title="测试报告的标题", description="测试报告的详情") result = runner.run(suite) result.testsRun #运行测试用例的总数 result.failure_count #运行失败的数目 print(result.success_count) #运行成功的数目 fp.close()
原文:https://www.cnblogs.com/xiuxiu123456/p/10938663.html