51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3226|回复: 5
打印 上一主题 下一主题

python下selenium测试报告整合(一)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2012-5-22 18:31:34 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
下载官网的 HTMLTestRunner.py 模块来集成selenium测试报告:使用方式如下

import StringIO
import sys
import unittest
import HTMLTestRunner
# ----------------------------------------------------------------------

def safe_unicode(obj, *args):
    """ return the unicode representation of obj """
    try:
        return unicode(obj, *args)
    except UnicodeDecodeError:
        # obj is byte string
        ascii_text = str(obj).encode('string_escape')
        return unicode(ascii_text)

def safe_str(obj):
    """ return the byte string representation of obj """
    try:
        return str(obj)
    except UnicodeEncodeError:
        # obj is unicode
        return unicode(obj).encode('unicode_escape')

# ----------------------------------------------------------------------
# Sample tests to drive the HTMLTestRunner

class SampleTest0(unittest.TestCase):
    """ A class that passes.

    This simple class has only one test case that passes.
    """
    def __init__(self, methodName):
        unittest.TestCase.__init__(self, methodName)

    def test_pass_no_output(self):
        """        test description
        """
        pass

class SampleTest1(unittest.TestCase):
    """ A class that fails.

    This simple class has only one test case that fails.
    """
    def test_fail(self):
        """
        test description (描述)
        """
        self.fail()

class SampleOutputTestBase(unittest.TestCase):
    """ Base TestCase. Generates 4 test cases x different content type. """
    def test_1(self):
        print self.MESSAGE
    def test_2(self):
        print >>sys.stderr, self.MESSAGE
    def test_3(self):
        self.fail(self.MESSAGE)
    def test_4(self):
        raise RuntimeError(self.MESSAGE)

class SampleTestBasic(SampleOutputTestBase):
    MESSAGE = 'basic test'

class SampleTestHTML(SampleOutputTestBase):
    MESSAGE = 'the message is 5 symbols: <>&"\'\nplus the HTML entity string: [&copy;] on a second line'

class SampleTestLatin1(SampleOutputTestBase):
    MESSAGE = u'the message is áéíóú'.encode('latin-1')

class SampleTestUnicode(SampleOutputTestBase):
    u""" Unicode (統一碼) test """
    MESSAGE = u'the message is \蕣'
    # 2006-04-25 Note: Exception would show up as
    # AssertionError: <unprintable instance object>
    #
    # This seems to be limitation of traceback.format_exception()
    # Same result in standard unittest.
    def test_pass(self):
        u""" A test with Unicode (統一碼) docstring """
        pass

# ------------------------------------------------------------------------
# This is the main test on HTMLTestRunner

class Test_HTMLTestRunner(unittest.TestCase):

    def test0(self):
        self.suite = unittest.TestSuite()
        buf = StringIO.StringIO()
        runner = HTMLTestRunner.HTMLTestRunner(buf)
        runner.run(self.suite)
        # didn't blow up? ok.
        self.assert_('</html>' in buf.getvalue())

    def test_main(self):
        # Run HTMLTestRunner. Verify the HTML report.

        # suite of TestCases
        self.suite = unittest.TestSuite()
        self.suite.addTests([
            unittest.defaultTestLoader.loadTestsFromTestCase(SampleTest0),
            unittest.defaultTestLoader.loadTestsFromTestCase(SampleTest1),
            unittest.defaultTestLoader.loadTestsFromTestCase(SampleTestBasic),
            unittest.defaultTestLoader.loadTestsFromTestCase(SampleTestHTML),
            unittest.defaultTestLoader.loadTestsFromTestCase(SampleTestLatin1),
            unittest.defaultTestLoader.loadTestsFromTestCase(SampleTestUnicode),
            ])

        # Invoke TestRunner
        buf = StringIO.StringIO()
        #runner = unittest.TextTestRunner(buf)       #DEBUG: this is the unittest baseline
        runner = HTMLTestRunner.HTMLTestRunner(
                    stream=buf,
                    title='<Demo Test>',
                    description='This demonstrates the report output by HTMLTestRunner.'
                    )
        runner.run(self.suite)

        # Define the expected output sequence. This is imperfect but should
        # give a good sense of the well being of the test.
        EXPECTED = u"""
Demo Test

>SampleTest0:

>SampleTest1:

>SampleTestBasic
>test_1<
>pass<
basic test

>test_2<
>pass<
basic test

>test_3<
>fail<
AssertionError: basic test

>test_4<
>error<
RuntimeError: basic test


>SampleTestHTML
>test_1<
>pass<
'the message is 5 symbols: \\x3C\\x3E\\x26\\"\\'\\n
plus the HTML entity string: [\\x26copy;] on a second line

>test_2<
>pass<
'the message is 5 symbols: \\x3C\\x3E\\x26\\"\\'\\n
plus the HTML entity string: [\\x26copy;] on a second line

>test_3<
>fail<
AssertionError: the message is 5 symbols: \\x3C\\x3E\\x26\\"\\'\\n
plus the HTML entity string: [\\x26copy;] on a second line

>test_4<
>error<
RuntimeError: the message is 5 symbols: \\x3C\\x3E\\x26\\"\\'\\n
plus the HTML entity string: [\\x26copy;] on a second line


>SampleTestLatin1
>test_1<
>pass<
the message is áéíóú

>test_2<
>pass<
the message is áéíóú

>test_3<
>fail<
AssertionError: the message is áéíóú

>test_4<
>error<
RuntimeError: the message is áéíóú


>SampleTestUnicode
>test_1<
>pass<
the message is \蕣

>test_2<
>pass<
the message is \蕣

>test_3<
>fail<
AssertionError: \\x3Cunprintable instance object\\x3E

>test_4<
>error<
RuntimeError: \\x3Cunprintable instance object\\x3E

Total
>19<
>10<
>5<
>4<
</html>
"""
        # check out the output
        byte_output = buf.getvalue()
        # output the main test output for debugging & demo
        print byte_output
        # HTMLTestRunner pumps UTF-8 output
        output = byte_output.decode('utf-8')
        self._checkoutput(output,EXPECTED)


    def _checkoutput(self,output,EXPECTED):
        i = 0
        for lineno, p in enumerate(EXPECTED.splitlines()):
            if not p:
                continue
            j = output.find(p,i)
            if j < 0:
                self.fail(safe_str('Pattern not found lineno %s: "%s"' % (lineno+1,p)))
            i = j + len(p)




##############################################################################
# Executing this module from the command line
##############################################################################

import unittest
if __name__ == "__main__":
    if len(sys.argv) > 1:
        argv = sys.argv
    else:
        argv=['test_HTMLTestRunner.py', 'Test_HTMLTestRunner']
    unittest.main(argv=argv)
    # Testing HTMLTestRunner with HTMLTestRunner would work. But instead
    # we will use standard library's TextTestRunner to reduce the nesting
    # that may confuse people.
    #HTMLTestRunner.main(argv=argv)


这个是将测试报告输出到标准到控制台,如果要将测试报告输出到文件当然也是可以的
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

  • TA的每日心情

    2017-10-22 19:44
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    6#
    发表于 2017-8-8 11:47:44 | 只看该作者
    。。。。。生成个测试报告  干嘛写的这么复杂
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2012-6-26 16:58:32 | 只看该作者
    直接跳过了 ,robotframework
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2012-5-31 19:43:47 | 只看该作者
    不错,说得好,大家鼓掌
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
    发表于 2012-5-28 16:19:01 | 只看该作者
    这是将报告打印在控制台啊。楼主所说的第二种方法是输出是接下来的输出到HTML么?
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    2#
     楼主| 发表于 2012-5-22 18:35:24 | 只看该作者
    还有第二种方法,有空再发。
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-26 21:03 , Processed in 0.088391 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表