python生成html报告

转自:http://blog.sina.com.cn/s/blog_893e15b70101fhg5.html

作者:滔滔   感谢作者!
即将开始一系列的自动化项目实践,很多公共类和属性都需要提前搞定。今天,解决了测试报告的一些难题,参照了很多博文,最终觉得HTMLTestRunner非常不错,自己也研读了源码,可进行一些自定义的配置。
下面就说一些体会:
一、配置:
HTMLTestRunner看了源码才知道作者是:Wai Yip Tung.到底这是哪位大神就不做深究了,至少我很敬佩。
1、下载HTMLTestRunner.py文件:地址 http://tungwaiyip.info/software/HTMLTestRunner.html
2、将该文件保存在python安装路径下的lib文件夹中。在文件中能import HTMLTestRunner成功,即配置成功。
注:如果失败,在项目中新建一个这样的文件也是可以的,只要达到能引入和使用就行。

二、使用
引入完成后,在调用测试文件中加入如下代码即可:
if __name__ == '__main__':
      testunit=unittest.TestSuite()    #定义一个单元测试容器
       testunit.addTest(element("test_find_element"))   #将测试用例加入到测试容器中
       testunit.addTest(element("test_search"))
      
      filename="./xxx.html"   #定义个报告存放路径,支持相对路径。
      fp=file(filename,'wb')
      runner =HTMLTestRunner.HTMLTestRunner(stream=fp,title='Report_title',description='Report_description')   #使用HTMLTestRunner配置参数,输出报告路径、报告标题、描述
      runner.run( testunit) #自动进行测试

生成报告如下:
[转载]python <wbr>webdriver测试报告

element:单元测试类名
test_find_element:测试用例方法名
详细信息:测试用例方法中print的数据、出错后信息。

附上单元测试里的一些方法:
常用方法:
assertEqual(a, b)     a == b      
assertNotEqual(a, b)     a != b      
assertTrue(x)     bool(x) is True      
assertFalse(x)     bool(x) is False      
assertIs(a, b)     a is b     #python 2.7
assertIsNot(a, b)     a is not b     #python 2.7
assertIsNone(x)     x is None      #python 2.7
assertIsNotNone(x)     x is not None      #python 2.7
assertIn(a, b)     a in b      #python 2.7
assertNotIn(a, b)     a not in b      #python 2.7
assertIsInstance(a, b)     isinstance(a, b)      #python 2.7
assertNotIsInstance(a, b)     not isinstance(a, b)      #python 2.7

用于执行更具体的检查,如:

Method     Checks that     New in
assertAlmostEqual(a, b)     round(a-b, 7) == 0      
assertNotAlmostEqual(a, b)     round(a-b, 7) != 0      
assertGreater(a, b)     a > b      #python 2.7
assertGreaterEqual(a, b)     a >= b      #python 2.7
assertLess(a, b)     a < b      #python 2.7
assertLessEqual(a, b)     a <= b      #python 2.7
assertRegexpMatches(s, re)     regex.search(s)      #python 2.7
assertNotRegexpMatches(s, re)     not regex.search(s)      #python 2.7
assertItemsEqual(a, b)     sorted(a) == sorted(b) and works with unhashable objs      #python 2.7
assertDictContainsSubset(a, b)     all the key/value pairs in a exist in b      #python 2.7
assertMultiLineEqual(a, b)     strings      #python 2.7
assertSequenceEqual(a, b)     sequences      #python 2.7
assertListEqual(a, b)     lists      #python 2.7
assertTupleEqual(a, b)     tuples      #python 2.7
assertSetEqual(a, b)     sets or frozensets      #python 2.7
assertDictEqual(a, b)     dicts      #python 2.7
assertMultiLineEqual(a, b)     strings     #python 2.7
assertSequenceEqual(a, b)     sequences      #python 2.7
assertListEqual(a, b)     lists      #python 2.7
assertTupleEqual(a, b)     tuples      #python 2.7
assertSetEqual(a, b)     sets or frozensets      #python 2.7
assertDictEqual(a, b)     dicts     #python 2.7

正在品读源码,下次在专门写下里面的东西。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值