如何在我的代码中使用python生成html测试报告

时间:2011-01-25 22:40:16

标签: python html testing report

from selenium import selenium
import unittest, time, re
import HTMLTestRunner

class Untitled(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost",4444,"*iexplore","http://google.com.ua/")
        self.selenium.start()

    def test_untitled(self):
        sel = self.selenium
        sel.open("/")
        sel.type("q", "home")
        sel.click("btnG")
        sel.click("link=Welcome to Home.com")
        sel.wait_for_page_to_load("30000") 
        self.failUnless(sel.is_text_present("Results * for selenium rc"))

    def tearDown(self):
        time.sleep(25)
 self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

2 个答案:

答案 0 :(得分:1)

当我遇到类似的问题时,我不得不覆盖TestResultTestCase.defaultTestResult()来将数据收集到一些方便我的列表中。然后你可以使用任何模板库(Jinja,Mako等)把它变成HTML。

答案 1 :(得分:-1)

你可以试试吗

if __name__ == "__main__":
  testSuite = unittest.TestSuite(suites)
  runner = unittest.TextTestRunner(verbosity=1)
  filename = "Test1.html"
  output = open (filename,"wb")
  runner = HTMLTestRunner.HTMLTestRunner(
         stream=output,
         title="Test report"
     )
  runner.run(testSuite)