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) #自动进行测试
生成报告如下:
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
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
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
更多请参见:http://docs.python.org/2/library/unittest.html
正在品读源码,下次在专门写下里面的东西。
相关文章