美文网首页
HTML 测试报告

HTML 测试报告

作者: 顾顾314 | 来源:发表于2018-06-14 16:02 被阅读27次

我最近更新文章比较频繁,介绍了比较多的测试方面的知识。其实主要是为了讲到今天的内容,为什么我急于讲到这个内容?测试工作做久了我们往往会有一个困惑,如何量化我们的工作价值?产品经理设计了更友善更实用的产品,研发人员用代码将这些功能实现,UI 工程师将这些功能以吸引人的方式体现出来,一个产品的形成他们总是被想到的功臣。测试人员总是被习惯性的忽视,但是产品出了问题,所有的矛头又都指向了测试人员。这都是他妈的什么玩意嘛。所以今天讲到的这个尽管他有很多用处,但是我认为他最重要的一个用处是测试人员价值的一个直观体现。

Usage

import HtmlTestRunner
import unittest


class TestStringMethods(unittest.TestCase):
        # Example test for HtmlRunner. 

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

    def test_error(self):
        """ This test should be marked as error one. """
        raise ValueError

    def test_fail(self):
        """ This test should fail. """
        self.assertEqual(1, 2)

    @unittest.skip("This is a skipped test.")
    def test_skip(self):
        """ This test should be skipped. """
        pass


if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='example_dir'))

上面这个示例是一个完整可执行的脚本,并可生成测试报告。
只需从包中导入HtmlTestRunner,然后使用关键字testRunner将其传递给unittest.main。这个类只有一个必需的参数,output是用来放置TestCase的报告的,它保存在一个reports目录下。

HTML 测试报告
report_title是显示在测试报告顶部的标题,比如上面的Test Result,如果用户不设置该参数,则使用该默认值。

对于拥有test suites(测试套件)的人来说,它也可以工作,只需创建一个runner实例,并使用您的套件调用run方法。下面一个例子:

from unittest import TestLoader, TestSuite
from HtmlTestRunner import HTMLTestRunner
import ExampleTest
import Example2Test

example_tests = TestLoader().loadTestsFromTestCase(ExampleTests)
example2_tests = TestLoader().loadTestsFromTestCase(Example2Test)

suite = TestSuite([example_tests, example2_tests])

runner = HTMLTestRunner(output='example_suite')

runner.run(suite)

这是runner默认使用的模板的结果示例。如果您想使用自己的模板,可以在实例化HtmlTestRunner类时将abolute路径传递给它,比如HtmlTestRunner(output='example_suite', template='path/to/template', report_title='My Report')。您的模板必须使用jinja2语法,因为这是我们使用的引擎。

HTMLTestRunner.py 下载地址。下载之后将该文件放到 Python 的安装目录下。

相关文章

网友评论

      本文标题:HTML 测试报告

      本文链接:https://www.haomeiwen.com/subject/ogrleftx.html