MindRetrieve Blog

MindRetrieve - an open source desktop search tool for your personal web

Monday, February 13, 2006

Announce: HTMLTestRunner - generates HTML test report for unittest

I'd like to annouce the release of Python library HTMLTestRunner, a by-product of the MindRetrieve project.

------------------------------------------------------------------------
HTMLTestRunner is an extension to the Python standard library's unittest
module. It generates easy to use HTML test reports. See a sample report at
http://tungwaiyip.info/software/sample_test_report.html.

Check more information and download from
http://tungwaiyip.info/software/#htmltestrunner

------------------------------------------------------------------------

Actually this was posted on comp.lang.python newsgroup a while ago. Thank you Cameron Laird for picking it up in Dr. Dobb's Python-URL!

83 Comments:

  • At 9:48 PM, Blogger Tung Wai Yip said…

    By BSD license. Feel free to use it in your project :)

     
  • At 4:44 PM, Anonymous Anonymous said…

    Hi,

    This is very nice!!!

    Is there a way to get the result into a file other than using:

    test_HTMLTestRunner.py > result.html

    Thanks!

     
  • At 6:35 PM, Blogger Tung Wai Yip said…

    Yes, HTMLTestRunner takes a parameter 'stream'. It is defaulted to sys.stdout but you can pass any file like object. It is similar to unittest.TextTestRunner.

     
  • At 1:15 AM, Anonymous Anonymous said…

    Great, thanks very much!

    I have another question: How can I pass a value to a test case object and make a test suite. I don't want to manually add each test function because that would be very labor intensive.

    # File MyCase.py
    import unittest


    class MyCase(unittest.TestCase):
    def __init__(self, value):
    super(MyCase, self).__init__()
    self.value = value
    def test1(self):
    print self.value
    def test2(self):
    print 'world'


    if __name__ == '__main__':
    msg = 'Hello'
    myCase = MyCase(msg)
    suite = unittest.TestSuite()
    suite.addTest(myCase)
    unittest.TextTestRunner(verbosity=2).run(suite)


    D:\MyWorks>MyCase.py
    Traceback (most recent call last):
    File "D:\MyWorks\MyCase.py", line 14, in ?
    myCase = MyCase(msg)
    File "D:\MyWorks\MyCase.py", line 5, in __init__
    super(MyCase, self).__init__()
    File "C:\Python24\lib\unittest.py", line 208, in __init__
    raise ValueError, "no such test method in %s: %s" % \
    ValueError: no such test method in [class '__main__.MyCase']: runTest

     
  • At 10:29 AM, Blogger Tung Wai Yip said…

    Unfortunately unittest makes it a lot harder that you would expect to do something like that, namely instantiate your own TestCase object and then build a TestSuite with all test methods. If you can jump through a few hoops using unittest.getTestCaseNames() your test can be written as:

    -----------------------------------
    import unittest

    class MyCase(unittest.TestCase):
    def __init__(self, methodName, value):
    super(MyCase, self).__init__(methodName)
    self.value = value
    def test1(self):
    print self.value
    def test2(self):
    print 'world'


    if __name__ == '__main__':
    msg = 'Hello'
    testnames = unittest.getTestCaseNames(MyCase,'test')
    suite = unittest.TestSuite()
    suite.addTests([MyCase(methodName, msg) for methodName in testnames])
    unittest.TextTestRunner(verbosity=2).run(suite)
    -----------------------------------

    In my opinion unittest makes things more complicated than necessary. This maybe part of the reason that spurs competiting framework like py.test.

     
  • At 11:18 AM, Anonymous Anonymous said…

    Thanks again for your prompt reply! It does seem quite complex...

    In your HTMLTestRunner, how can I add a vertical scroll-bar to the test result pop-up window of [error, fail, pass]? Text should automatically wrap horizontally. I need to dump the log file of my software to each test and have one single report for all the tests.

    I am writing a hiarachical test suite, which I have a set of commmon test files that must be executed by multiple environments. To give you some example,
    suite = TestSuite()
    for ip in ips:
    for com in ['com1', 'com2']:
    for case in ['common1', 'common2']:
    t = case(ip, com)
    suite.addTest(t)

    htmlRunner.run(suite, 'TestReport.html')

    Best regards,
    Podi

     
  • At 8:39 PM, Blogger Tung Wai Yip said…

    The absence of scroll bar is a bug. Thanks for bringing this up. A fix is available in version 0.7.1. Unfortunately I don't know of anyway to wrap long line in HTML yet preserving the pre-formatting. Unless you are using Opera, which does have a very nice 'Fit to screen width' format option.

     
  • At 12:55 AM, Anonymous Anonymous said…

    Hi I found a small bug:

    The link to test_HTMLTestRunner.py
    points actually to:
    HTMLTestRunner.py

     
  • At 10:11 AM, Blogger Tung Wai Yip said…

    You're right. I've fixed the wrong link. Thank you.

     
  • At 12:41 AM, Blogger Mirko said…

    I usually use inheritance for this:

    # File MyCase.py
    import unittest

    class MyCase(unittest.TestCase):
    value = 'Hello'

    def test1(self):
    print self.value

    def test2(self):
    print 'world'

    class MySecondCase(MyCase):
    value = 'Good bye'

    if __name__ == '__main__':
    suite = unittest.TestSuite()
    for case in MyCase, MySecondCase:
    # using obsolete function still available in 2.4
    suite.addTest(unittest.makeSuite(case))
    unittest.TextTestRunner(verbosity=2).run(suite)

     
  • At 1:46 PM, Anonymous Anonymous said…

    How do I get the docstring of the testcase into the HTML? E.g.:

    def test_x(self):
    ____"""Hello greets"""
    ____print "hello"

    With the TextTestRunner, I use "verbosity=2" to get it in the output, but this does not work in HTMLTestRunner.

    Apart from that, I very much like HTMLTestRunner, thanks for it!

     
  • At 10:02 PM, Blogger Tung Wai Yip said…

    Thank you Wolfgang for sending a patch to output the docstring descriptions. I have posted it as version 0.8.1.

     
  • At 10:07 PM, Blogger Tung Wai Yip said…

    I see this comment is becoming a support forum. The number of comments are getting hard to follow. I suggest you just email me directly for support (see my contact).

    In the mean time I would try to find a better support option, perhaps turning this into a full fledge open source project. Let me know if you want to see this happens.

     
  • At 7:45 AM, Blogger Wang Qi said…

    I tried out your example. It rocks! Plan to try out in the real example.

     
  • At 4:16 AM, Anonymous Anonymous said…

    Hi,
    I am using Python 2.5..Can you please tell me, how to install this HTMLTestRunner.py ?

    Do I need to save this file on my python 2.5 folder

     
  • At 4:22 AM, Anonymous Anonymous said…

    Hi,
    I am running many test cases on single suite:

    Example:
    suite = unittest.TestLoader().loadTestsFromTestCase(class_name)
    unittest.TextTestRunner().run(suite)

    When I do import HTMLTestRunner


    I need to modify this code to:
    (instead of TextRunner)

    runner = HTMLTestRunner.HTMLTestRunner()
    runner.run(suite)

    Does this work?..or can you please help me out..

     
  • At 10:28 AM, Anonymous Anonymous said…

    I am having trouble getting it to work here is my code. I must be missing something?

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


    class Untitled(unittest.TestCase):
    def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*chrome", "http://change-this-to-the-site-you-are-testing/")
    self.selenium.start()

    def test_untitled(self):
    reader = csv.reader(open("mydata.csv", "rb"))
    for row in reader:
    sel = self.selenium
    sel.open("http://ushadma-myacctd/MyAccountUAT/SignOn.aspx?ReturnUrl=%2fMyAccountUAT%2fdefault.aspx")
    sel.type("ctl00_MyAccountContent_SignIn_txtUsername", row[0])
    sel.type("ctl00_MyAccountContent_SignIn_txtPassword", row[1])
    sel.click("ctl00_MyAccountContent_SignIn_btnLogin")
    sel.wait_for_page_to_load("30000")
    sel.click("//form[@id='aspnetForm']/div[3]/center/table/tbody/tr[2]/td[3]/div[1]/a")
    sel.wait_for_page_to_load("30000")
    sel.click("link=Log Out")
    sel.wait_for_page_to_load("30000")

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

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

     
  • At 8:20 AM, Blogger Jo said…

    Hi, Thank you very much. I am wondering is there anyway, if the test fails, get a screenshot of the page and add a link to it in the result?

    Thank you
    Jo

     
  • At 4:52 PM, Blogger Tung Wai Yip said…

    Welcome Selenium users. I hope this tool can help you in presenting your test result. I was helping some users in here before. As I have stated because this is not a full blown open source project I find it difficult to provide support in the comment section. You can always contact me directly. But I wonder if there is an infrastructure under Selenium that can better facilitate technical support for this third party tool. If you can help set this up I'll happy to drop by to provide support and even enhancement.

    Thank you. Happy testing!

     
  • At 9:10 AM, Anonymous Anonymous said…

    i am trying write below code

    =================================

    import HTMLTestRunner
    import unittest
    from main import TestDefaultServer

    class RunAllTests(unittest.TestCase):
    def run_all_tests(self):
    self.suite = unittest.TestSuite()
    self.suite.addTests([
    unittest.defaultTestLoader.loadTestsFromTestCase(TestDefaultServer)])
    runner = HTMLTestRunner.HTMLTestRunner()
    runner.run(self.suite, 'my_report.html')


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

    ===================================

    when I run it I only see

    Ran 1 test in 61.109s

    OK

    It is the standard pyunit output.
    But my_report.html didn't generated.
    What's wrong in my code?

     
  • At 10:53 PM, Anonymous Anonymous said…

    @Ievhen: Change Python to 2.5.x. I had the same problem - after downgrading to 2.5 branch, it started working.

     
  • At 11:23 AM, Anonymous Anonymous said…

    Thank you so much- wasn't looking forward to writing this, and was happy to see someone wrote it already.

    Thanks again, really great!

     
  • At 10:04 PM, Blogger Nitin K said…

    Hello Wai,

    Using HTMLTestRunner i am getting the report similar to one example on your main page. But i want to add one more column 'skip' similar to pass fail and error because there are many test cases which need to be skipped according to condition/requirment.
    Please suggest.

    Nitin K

     
  • At 9:09 AM, Blogger Unknown said…

    Thanks man, this is very nice
    You saved me lot of time.

     
  • At 3:55 PM, Anonymous Anonymous said…

    The download pages seem to be an unformatted string of commentary and python code. Is this messed up, or is this some new style of file download?

     
  • At 4:00 PM, Anonymous Anonymous said…

    The downloads work fine in Firefox. It is just messed up in IE.

     
  • At 12:44 PM, Anonymous Anonymous said…

    Not sure if anyone ends up here anymore, however, I had a weird issue where one module, for some reason, was erroring on line 757 in the function: _generate_report_test(self, rows, cid, tid, n, t, o, e):
    # e.g. 'pt1.1', 'ft1.1', etc
    has_output = bool(o or e)
    tid = (n == 0 and 'p' or 'f') + 't%s.%s' % (cid+1,tid+1)
    name = t.id().split('.')[-1] <-- this line

    For some reason t.id() was empty
    so modified this line with a try block as follows:

    try:
    name = t.id().split('.')[-1]
    except:
    name =str(t).split(" ")[0]

    I still have no idea the cause for that one test module..

     
  • At 7:55 AM, Blogger rprancan@gmail.com said…

    Here is a file I'm trying to run as a complete newbie and it doesn't work. Can you help me.

    #!/usr/bin/env python
    # encoding: utf-8
    """
    untitled.py

    Created by xxxxx on 2012-01-05.
    Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    """

    import sys
    import os
    import unittest
    import HTMLTestRunner
    print "hello"
    def setUp(self):
    popup("setting things up")
    def test4(self):
    print "test1"
    def test5(self):
    print "test2"
    def test1(self):
    print ("running test1")
    def test2(self):
    print ("running test2")
    def test3(self):
    print ("running test3")
    def tearDown(self):
    popup("tearing things down")
    print "Goodby"

    if __name__ == '__main__':
    HTMLTestRunner.main()

     
  • At 7:18 PM, Anonymous Anonymous said…

    Thanks!
    Your work is very nice!!!
    I love this module.

     
  • At 12:37 AM, Blogger KVG said…

    Can anyone tell me how to get a result in which it is mentioned pass/fail test case wise for python. Like if we can do some modifications in testrunner.py file. It will be very helpful for me.
    Thanx!!

     
  • At 5:31 PM, Anonymous Rado said…

    To correctly import HTMLTestRunner.py where should it be placed(under which directory) and how do you append sys.path in python for this HTMLTestRunner.py file

     
  • At 1:00 AM, Anonymous Rado said…

    Is @unittest.skip() functionality also supported, meaning seems like reports for skipped tests are not generated, correct?

     
  • At 5:00 AM, Anonymous Anonymous said…

    Hi, I'm having problems with test discovery. Report is generated if the test file is run directly, but no report if I use python -m unittest discover. I'm following this example:

    https://gist.github.com/1073424

    What do I need to do to make it work with test discovery ?

     
  • At 11:47 AM, Anonymous Stu said…

    If I run multiple tests pointing to the same same output file, i get multiple tabels, one fore each time it is called, which if great, but the links for Pass always open the details in teh first table.

    suite=unittest.TestLoader().loadTestsFromTestCase(unitTestSample)
    print suite
    outfile = open("C:\Report.html", "w")
    runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title='Test Report', description='This is test suite 1' )
    runner.run(suite)
    # unittest.TextTestRunner(verbosity=2).run(suite)

    suite2=unittest.TestLoader().loadTestsFromTestCase(unitTestSample2)
    print suite2
    runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title='Test Report Part 2', description='This is test suite 2' )
    runner.run(suite2)
    # unittest.TextTestRunner(verbosity=2).run(suite2)

     
  • At 1:17 AM, Anonymous Avinash said…

    Hi,
    I want to use this HTMLTestRunner utility with py.test or nose framework. Can you post some example for these frameworks?

     
  • At 1:03 PM, Blogger Unknown said…

    Hi,

    from selenium import selenium
    import HTMLTestRunner
    import unittest, time

    class main_script(unittest.TestCase):
    selenium = selenium("localhost", 4444, "*firefox","http://google.co.in")
    selenium.start()
    selenium.open("/")
    def test01_script(self):
    browser=self.selenium
    print "Open the google page"
    browser.type("q", "selenium RC")
    print "Enter the search test Selenium RC"
    browser.click("btnG")
    print "Click on Google Search button"
    browser.stop()
    if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(main_script))
    dateTimeStamp = time.strftime('%Y%m%d_%H_%M_%S')
    buf = file("TestReport" + "_" + dateTimeStamp + ".html", 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(
    stream=buf,
    title='Test the Report',
    description='Result of tests'
    )
    runner.run(suite).


    The script does not run completley. its just open the browser and close. it does not went to test function. could you please tell me what peace of is missing.

    Thanks
    HariRavi

     
  • At 3:16 PM, Anonymous Anonymous said…

    Hi,

    I am getting following error.

    Can someone help me in resolving this.

    self.suite.addTests([
    unittest.defaultTestLoader.loadTestsFromTestCase(SummaryTest)
    ])
    # Invoke TestRunner
    buf = StringIO.StringIO()

    #runner = unittest.TextTestRunner(buf) #DEBUG: this is the unittest baseline
    runner = HTMLTestRunner(
    stream=buf,
    title='TestReport',
    > description='This demonstrates the report output by HTMLTestRunner.'
    )
    E TypeError: 'module' object is not callable

     
  • At 3:30 PM, Anonymous Anonymous said…

    Sorry guys ignore my previous post regarding

    TypeError: 'module' object is not callable

    I found the issue. I was doing silly mistake.

    Thanks

     
  • At 5:13 AM, Blogger alexpf said…

    This comment has been removed by the author.

     
  • At 5:18 AM, Blogger alexpf said…

    I use Sikuli
    Code:
    img = capture(Region(find(Pattern("img1.png"))#get screenshot
    shutil.move(img, os.path.join("%HOME%", "img.png"))

    in Sikuli IDE it work
    But in HTMLTestRunner don't work:
    shutil.move(img, os.path.join("%HOME%", "img.png"))

    it can not write the file to disk, it seems to me
    what's wrong?

     
  • At 4:22 PM, Anonymous Anonymous said…

    Hi,

    I am using HTMLrunner.py to run py.test cases. I could able to generate the report. But when I am clicking on Detail in the report nothing happens. On the console of the browser I could see below error.
    "Uncaught ReferenceError: showClassDetail is not defined "

    Then I try to go in the HTML to make sure java script is present on the page. After updating class of pt1.1 to empty earlier it was hidden. IT worked fine for me.

    Did someone else got this issue. I am new to java script dont know how to debug it. Can some one please let me know how to fix this.

    Thanks in advance.

     
  • At 9:23 PM, Blogger creations said…

    Hi,

    This is very helpful for me. Thanks for developed this tool.

    I am facing some issue. The issue is some exceptions are not showing in Htmltestrunner output file

    For example: if make any import error or getting nosuchelement exception,for these errors in the output file it is coming as test results passed.can someone please help me on this.

    Thanks in advance.

     
  • At 8:36 AM, Blogger Unknown said…

    This comment has been removed by the author.

     
  • At 8:37 AM, Anonymous Anonymous said…

    While running python's unittest module with differrent tests using the HTMLTestRunner, it sometimes craches with MemoryError Exception SystemOutOfMemory.

    I've noticed it happens when report size exceeds 30MB approx.

    Have you seen such an issue ?

    It also specified that crash happened in _generate_report method.

     
  • At 7:17 AM, Blogger eddo said…

    Hi all,
    Is it possible to repeat failed tests (ex. for 3 times) if test fails because of environment or email problem or something like this.

    Thanks...

     
  • At 4:38 AM, Blogger Wiki This... said…

    Tried to install and failedsudo pip install http://tungwaiyip.info/software/HTMLTestRunner.html
    Downloading/unpacking http://tungwaiyip.info/software/HTMLTestRunner.html
    Downloading HTMLTestRunner.html
    Cleaning up...
    Exception:
    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
    File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 278, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
    File "/usr/local/lib/python2.7/dist-packages/pip/req.py", line 1197, in prepare_files
    do_download,
    File "/usr/local/lib/python2.7/dist-packages/pip/req.py", line 1375, in unpack_url
    self.session,
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 582, in unpack_http_url
    unpack_file(temp_location, location, content_type, link)
    File "/usr/local/lib/python2.7/dist-packages/pip/util.py", line 627, in unpack_file
    and is_svn_page(file_contents(filename))):
    File "/usr/local/lib/python2.7/dist-packages/pip/util.py", line 210, in file_contents
    return fp.read().decode('utf-8')
    File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
    UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte

    Storing debug log for failure in /home/jstewart/.pip/pip.log

     
  • At 4:41 AM, Blogger Wiki This... said…

    Tried to install as follows but failed
    sudo pip install http://tungwaiyip.info/software/HTMLTestRunner.html
    Downloading/unpacking http://tungwaiyip.info/software/HTMLTestRunner.html
    Downloading HTMLTestRunner.html
    Cleaning up...
    Exception:
    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
    File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 278, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
    File "/usr/local/lib/python2.7/dist-packages/pip/req.py", line 1197, in prepare_files
    do_download,
    File "/usr/local/lib/python2.7/dist-packages/pip/req.py", line 1375, in unpack_url
    self.session,
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 582, in unpack_http_url
    unpack_file(temp_location, location, content_type, link)
    File "/usr/local/lib/python2.7/dist-packages/pip/util.py", line 627, in unpack_file
    and is_svn_page(file_contents(filename))):
    File "/usr/local/lib/python2.7/dist-packages/pip/util.py", line 210, in file_contents
    return fp.read().decode('utf-8')
    File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
    UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte

    Storing debug log for failure in /home/jstewart/.pip/pip.log

     
  • At 7:28 AM, Blogger Unknown said…

    Hi,

    I'm running my automation using selenium with python bindings. When i started using HTMLTestRunner, tests are being run twice. Any idea why it is happening? how to avoid the test running twice and also get the report.

    Its such a great way to get the report like this. Anyway to capture the screen shot of the failure too?

    Thanks,
    Sunitha

     
  • At 3:22 AM, Anonymous Alan said…

    hi has this been updated to run with the latest version of python?

    I am receiving the error with stringIO as this is no longer supported in python 3.

     
  • At 4:20 AM, Blogger jude augustine job said…

    import error:no nodule named 'StringIO'

     
  • At 5:01 AM, Anonymous VInu said…

    Sir, please help me to set HTMLTestRunner in python 3

    Its not working Python 3

     
  • At 10:51 PM, Blogger jude augustine job said…

    how do you HTMLTestRunner in python 3.4.2

     
  • At 9:16 AM, Anonymous Anonymous said…

    Hello,
    When i use HTMLTestRunner to run a test suite that contains multiple test cases, it generates a report but the report shows pass and no errors even though one of the test case fails.

     
  • At 3:43 AM, Anonymous Anonymous said…

    I am using the HTMLTestRunner to generate the HTML test report for unittest in sikuli.It is generating the report as expected but i have an additional requirement that after the report is genereated i also need to generate a text file that tells me the total number of scripts executed,passed,failed and errored along with the name of scripts failed and errored.

    I am able to retrieve the details of the total number of scripts executed,passed,failed and errored by using the result object returned by the HTMLTestRunner .I have used the following code.

    def main():
    today = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    startDate = str(today)
    dir = "C:/Reports/"
    filename = startDate+"_TestSuite_Report.html"
    print filename
    fp = file(os.path.join(dir, filename), "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream = fp, verbosity=2)
    result = runner.run(suite())
    fp.close()
    count = str(result.success_count+result.failure_count+result.error_count),
    Pass = str(result.success_count),
    fail = str(result.failure_count),
    error = str(result.error_count),
    text_file = open(dir+"/Summary.txt", "a")
    text_file.write("Summary of the Scripts: "+filename + "\n")
    text_file.write("Total Scripts executed : %s" % count + "\n")
    text_file.write("Scripts Passed : %s" % Pass + "\n")
    text_file.write("Scripts Failed : %s" % fail + "\n" )
    text_file.write("Scripts Error : %s" % error + "\n" )
    text_file.close()

    Let me know if i could also access the name of the scripts failed through the result object. Thanks in advance.

     
  • At 9:23 AM, Blogger neu2006 said…

    This comment has been removed by the author.

     
  • At 9:25 AM, Anonymous Mahsum said…

    Hi,
    When run single file(with "python TestMyClass.py" command", it generates files properly. But when run multiple files(python -m unittest discover) cannot generate any report.

     
  • At 4:03 PM, Anonymous Anonymous said…

    If you are still working on this, please consider a verbosity level 3 that prints the error/failure class and line number

    Cheers

     
  • At 3:10 AM, Anonymous Anonymous said…

    >c:\python27\pythonw -u "test_HTMLTestRunner.py"

    Time Elapsed: 0:00:00
    ..F..FE..FE..FE..FE.
    Time Elapsed: 0:00:00.005000
    E
    ======================================================================
    ERROR: test_main (__main__.Test_HTMLTestRunner)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "test_HTMLTestRunner.py", line 219, in test_main
    print byte_output
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 14019-14023: ordinal not in range(128)

    ----------------------------------------------------------------------
    Ran 2 tests in 0.007s

    FAILED (errors=1)

     
  • At 3:25 AM, Blogger danibudi said…

    This comment has been removed by the author.

     
  • At 3:30 AM, Blogger danibudi said…

    Index: HTMLTestRunner.py
    ===================================================================
    --- HTMLTestRunner.py (revision 899)
    +++ HTMLTestRunner.py (working copy)
    @@ -1,3 +1,4 @@
    +# encoding: utf-8
    """
    A TestRunner for use with the Python unit testing framework. It
    generates a HTML report to show the result at a glance.
    @@ -115,14 +116,20 @@
    self.fp = fp

    def write(self, s):
    - self.fp.write(s)
    + self.fp.write(self._utf8(s))

    def writelines(self, lines):
    - self.fp.writelines(lines)
    + self.fp.writelines(map(self._utf8, lines))

    def flush(self):
    self.fp.flush()

    + @staticmethod
    + def _utf8(s):
    + if isinstance(s, unicode):
    + s = s.encode('utf8')
    + return s
    +
    stdout_redirector = OutputRedirector(sys.stdout)
    stderr_redirector = OutputRedirector(sys.stderr)

    @@ -684,7 +691,7 @@
    report = report,
    ending = ending,
    )
    - self.stream.write(output.encode('utf8'))
    + self.stream.write(output)


    def _generate_stylesheet(self):
    @@ -752,6 +759,16 @@

    def _generate_report_test(self, rows, cid, tid, n, t, o, e):
    # e.g. 'pt1.1', 'ft1.1', etc
    + try:
    + n = repr(n)
    + #~ print 'N=', n
    + n = int(n)
    + except Exception as e:
    + n=2
    + try:
    + print "n =", e.message
    + except Exception as ee:
    + print "type(e)=", type(e)
    has_output = bool(o or e)
    tid = (n == 0 and 'p' or 'f') + 't%s.%s' % (cid+1,tid+1)
    name = t.id().split('.')[-1]
    @@ -820,5 +837,12 @@
    # Executing this module from the command line
    ##############################################################################
    I received UnicodeDecodeError:
    Traceback (most recent call last):
    ...
    File "C:\python_tests\PromoLinks\src\com\selenium\tests\HTMLTestRunner.py", line 649, in run
    self.stopTime = datetime.datetime.now()
    File "C:\python_tests\PromoLinks\src\com\selenium\tests\HTMLTestRunner.py", line 696, in generateReport
    heading = self._generate_heading(report_attrs)
    File "C:\python_tests\PromoLinks\src\com\selenium\tests\HTMLTestRunner.py", line 760, in _generate_report
    for tid, (n,t,o,e) in enumerate(cls_results):
    File "C:\python_tests\PromoLinks\src\com\selenium\tests\HTMLTestRunner.py", line 806, in _generate_report_test
    script = script,
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position ...
    I've written a small fix that works for me. Bellow is the diff:

    +class UTF8(unittest.TestCase):
    + def test1(self):
    + "боза"
    +
    + def test2(self):
    + "проба"
    +
    if __name__ == "__main__":
    - main(module=None)
    + main()

     
  • At 3:39 AM, Blogger Unknown said…

    Respected Sir,

    How can we edit trace-back of the fail/error generated in the HTMLreporter.
    I would like to add a screenshot taken from Selenium to the report generated.

    Please help.

    Expecting your positive Response.

     
  • At 11:42 AM, Anonymous Anonymous said…

    I have compatibility issues to use HTMLTestRunner in Python3.X, any suggestions?

     
  • At 1:29 AM, Anonymous Anonymous said…

    Hi,

    I am wondering if HTMLTestRunner can rerun the failed cases with given repeat times, then generate the HTML result. Any help will be appreciate! Thank you!

     
  • At 11:50 AM, Anonymous Anonymous said…

    Yes, I have created a subclass that does so.

    Something like:

    def run(self, testSuite, pleaseRetryFailedTests=True):
    self.testResult = _TestResult(self.verbosity, self)
    self.testSuite = testSuite

    startTime = time.time()

    self.pleaseRetryFailedTests = pleaseRetryFailedTests

    try:
    self.testSuite(self.testResult)

    if self.testResult.failure_count > 0 and self.pleaseRetryFailedTests:
    print "\nRerunning %d failed tests" % self.testResult.failure_count

    self.pleaseRetryFailedTests = False

    failedTests = unittest.TestSuite()
    for result in self.testResult.results:
    if result[0] == 1:
    failedTests.addTest(result[1])
    self.testResult.results.remove(result)

    failedTests(self.testResult) # rerun failed tests, once

    except KeyboardInterrupt:
    pass

     
  • At 8:42 PM, Anonymous Anonymous said…

    Hello,

    Is it possible to use this report with pytest?

     
  • At 9:32 AM, Blogger Nerrad said…

    Thank you for providing this very useful module. I am using Python 3 now and want to use this program. I simply used the 2to3 utility provided with Python3, which fixed a few things. Then fix one more thing as below. Then you should use this tool with Python 3. Hope this helps.

    if isinstance(o,str):
    # TODO: some problem with 'string_escape': it escape \n and mess up formating
    # uo = unicode(o.encode('string_escape'))
    uo = o #xw .decode('latin-1')
    else:
    uo = o
    if isinstance(e,str):
    # TODO: some problem with 'string_escape': it escape \n and mess up formating
    # ue = unicode(e.encode('string_escape'))
    ue = e #xw .decode('latin-1')
    else:
    ue = e

     
  • At 10:29 PM, Blogger Unknown said…

    may i ask why not continue to maintain?

     
  • At 12:27 PM, Blogger Sathisha said…

    HI

    I am getting as error for my code while running the HTMLTestRunner

    C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.2\helpers\pycharm\noserunner.py" D:\PageObjectModel\PageObjectModel\testcases\add_new_partner.py::Untitled::test_untitled
    Testing started at 00:52 ...
    Error
    Traceback (most recent call last):
    File "C:\Python27\lib\unittest\case.py", line 329, in run
    testMethod()
    File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
    File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
    File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
    File "D:\PageObjectModel\PageObjectModel\testcases\add_new_partner.py", line 9, in
    import HTMLTestRunner
    File "D:\PageObjectModel\PageObjectModel\HTMLTestRunner.py", line 105, in
    from sikuli import *
    ImportError: No module named sikuli
    ===================================================================
    ERROR: Failure: ImportError (No module named sikuli)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
    File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
    File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
    File "D:\PageObjectModel\PageObjectModel\testcases\add_new_partner.py", line 9, in
    import HTMLTestRunner
    File "D:\PageObjectModel\PageObjectModel\HTMLTestRunner.py", line 105, in
    from sikuli import *
    ImportError: No module named sikuli

    ----------------------------------------------------------------------
    Ran 1 test in 0.003s

    FAILED (errors=1)

    Process finished with exit code 0

     
  • At 11:04 PM, Blogger Unknown said…

    Hi Wai Yip Tung,

    I have a very silly doubt. Your HTML test runner.py is in side my scrip folder. Its too good for reporting. I was able to successfully generate the report file too. But when I open the report, the count, pass, fail, error columns all show 0. Is there some changes I would need to make in HTML Testrunner.py file ?

    The second doubt is I am forced to indent all my code following the

    from sikuli import *
    import unittest
    import HTMLTestRunner
    class channel(unittest.TestCase):
    My code
    suite = unittest.TestLoader().loadTestsFromTestCase(channel)
    outfile = open("G:\\SIKULI\\Report\\report.html", "w") # path to report folder
    runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title=' Simulation1-SingleZoneVav', description='Test Report' )
    runner.run(suite)

    Because if I dont do this, then I get an indentation error while interpreting first line of my actual code

     
  • At 9:20 PM, Anonymous Vikas said…

    I have created this Python 3 version http://bit.ly/2rtjFPE

     
  • At 4:04 PM, Anonymous Anonymous said…

    Does it work with subTest like below? Will it show all the sub tests?

    class NumbersTest(unittest.TestCase):

    def test_even(self):
    """
    Test that numbers between 0 and 5 are all even.
    """
    for i in range(0, 6):
    with self.subTest(i=i):
    self.assertEqual(i % 2, 0)

     
  • At 8:38 AM, Blogger Unknown said…

    This comment has been removed by the author.

     
  • At 7:37 AM, Anonymous Anonymous said…

    I like the format of the HTML Test Report.
    It has an HTML link for when the test case has a 'fail' and also 'error'.
    How did you make it so 'pass' shows a link too?

     
  • At 5:19 AM, Anonymous Anonymous said…

    self.stream.write(output.encode('utf8'))
    TypeError: write() argument must be str, not bytes

     
  • At 6:43 PM, Anonymous Anonymous said…

    I am using it for the first time with Python 2.7 and seeing this error:

    HTMLTestRunner.py", line 685, in generateReport
    ending = ending,
    KeyError: 'reporting'

    Has anyone seen it, how to resolve? thanks!

     
  • At 7:56 PM, Anonymous Anonymous said…

    Solved my own problem, the key should be 'reporting' not 'report' as in the original file.

     
  • At 8:41 AM, Blogger Unknown said…

    I get this error and I also try pip install numpy but still it give me same error.
    I use pip version:18.1

    error:


    Collecting HTMLTestRunner

    Could not find a version that satisfies the requirement HTMLTestRunner (from versions: )
    No matching distribution found for HTMLTestRunner

     
  • At 8:47 AM, Blogger prashant_tolanur said…

    When I ran Suite, I got following error
    ModuleNotFoundError: No module named 'StringIO'
    ERROR: Module: SeleniumPythonTestSuite could not be imported (file: D:\SOFT\eclipse-jee-mars-2-win32-x86_64\Workspace\src\SeleniumPythonTestSuite.py).
    done.

     
  • At 9:14 AM, Blogger prashant_tolanur said…

    For Python 3, I updated the code :

    # instead of: import StringIO

    import io

    # A few lines later...

    # instead of portfolio_zip = StringIO.StringIO()

    portfolio_zip = io.BytesIO()

     
  • At 7:38 AM, Blogger Unknown said…

    I tried it is not working on python 3.7

     
  • At 6:10 AM, Anonymous Anonymous said…

    Is there any way to use HTML testrunner in a script which does not uses unittest ?

     
  • At 10:58 AM, Blogger Unknown said…

    I am getting following errors.
    can't invoke "event" command: application has been destroyed
    while executing
    "event generate $w <>"
    (procedure "ttk::ThemeChanged" line 6)
    invoked from within
    "ttk::ThemeChanged"
    can't invoke "event" command: application has been destroyed
    while executing
    "event generate $w <>"
    (procedure "ttk::ThemeChanged" line 6)
    invoked from within
    "ttk::ThemeChanged"
    can't invoke "event" command: application has been destroyed
    while executing
    "event generate $w <>"
    (procedure "ttk::ThemeChanged" line 6)
    invoked from within
    "ttk::ThemeChanged"

     
  • At 4:04 PM, Blogger Enji Cooper said…

    This module doesn't work with python3.

    I stumbled across this while looking into some source code that was in a codebase at work.

    Given that most of the python world is moving towards pytest, it might be a good idea to recommend that folks (instead) switch to pytest and install the pytest-html plugin: https://pypi.org/project/pytest-html/ .

     

Post a Comment

<< Home