运行少量case时
1 # coding = utf-8 2 3 from selenium import webdriver 4 import unittest 5 import time 6 from HTMLTestRunner import HTMLTestRunner 7 8 class MyTest(unittest.TestCase): 9 '''百度搜索测试'''10 11 def setUp(self):12 self.driver = webdriver.Firefox()13 self.base_url = "http://www.baidu.com/"14 15 def test_baidu(self):16 '''搜索关键字:unittest'''17 driver = self.driver18 driver.get(self.base_url + '/')19 driver.find_element_by_id("kw").clear()20 driver.find_element_by_id("kw").send_keys("unittest")21 driver.find_element_by_id("su").click()22 time.sleep(2)23 title = driver.title24 self.assertEqual(title, "unittest_百度搜索")25 26 def test_baidu2(self):27 '''搜索关键字:HTMLTestRunner'''28 driver = self.driver29 driver.get(self.base_url + '/')30 driver.find_element_by_id("kw").clear()31 driver.find_element_by_id("kw").send_keys("HTMLTestRunner")32 driver.find_element_by_id("su").click()33 time.sleep(2)34 title = driver.title35 self.assertEqual(title, "unittest_百度搜索")36 37 def tearDown(self):38 self.driver.quit()39 40 if __name__ == '__main__':41 testunit = unittest.TestSuite()42 testunit.addTest(MyTest("test_baidu"))43 testunit.addTest(MyTest("test_baidu2"))44 45 now = time.strftime("%Y-%m-%d %H_%M_%S")46 47 filename = './'+ now +'result.html'48 49 fp = open(filename, 'wb')50 runner = HTMLTestRunner(stream=fp,51 title='百度搜索测试报告',52 description='用例执行情况: ')53 54 runner.run(testunit)55 fp.close()
结果报告
运行所有case时
1 # coding = utf-8 2 3 import unittest, time 4 from HTMLTestRunner import HTMLTestRunner 5 6 7 test_dir = './' 8 discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') 9 10 if __name__ == "__main__":11 12 now = time.strftime("%Y-%m-%d %H_%M_%S")13 filename = './Report/'+ now +'result.html'14 fp = open(filename, 'wb')15 runner = HTMLTestRunner(stream=fp,16 title='测试报告',17 description='用例执行情况: ')18 19 runner.run(discover)20 fp.close()
测试结果报告为
报告中有下图框起来的字样是因为Python的注释doc string。 即在类或者方法的下放用三引号(""" """或''' ''')来添加doc string类型的注释。如第一个代码所示