python下selenium测试报告整合

使用过一段时间的Robot Framework测试框架,测试之前需要先搭环境,需要安装的东西很多,这一点个人有些排斥。

每一个测试内容对应一个Test_Case,Robot有自己语法格式,如判断、循环。实际使用中发现导入自定义Lib莫名报错,很是头疼。

脚本编写完成,可以在UI界面运行,也可以使用命令行pybot + Robot项目运行。

Robot最大的优点,自动生成html格式的测试报告,免去脚本中一大堆测试结果处理、保存动作。缺点上文已描述,需要搭建环境,自定义语法个人很排斥。

于是,这几天在网上寻找测试报告模板,最终觉得HTMLTestRunner非常不错。

一、下载、配置

1.下载HTMLTestRunner.py文件:地址http://tungwaiyip.info/software/HTMLTestRunner.html

2.将该文件保存在python安装路径下的lib文件夹中。

二、使用

1.基本用法

1.1 import unittest
1.2 定义一个继承自unittest.TestCase的测试用例类
1.3 定义setUp和tearDown,在每个测试用例前后做一些辅助工作。
1.4 定义测试用例,名字以test开头。
1.5 一个测试用例应该只测试一个方面,测试目的和测试内容应很明确。主要是调用assertEqual、assertRaises等断言方法判断程序执行结果和预期值是否相符。
1.6 调用unittest.main()启动测试
1.7 如果测试未通过,会输出相应的错误提示。如果测试全部通过则不显示任何东西,这时可以添加-v参数显示详细信息。

2.unittest模块的常用方法

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     2.7
assertIsNot(a, b)     a is not b     2.7
assertIsNone(x)     x is None     2.7
assertIsNotNone(x)     x is not None     2.7
assertIn(a, b)     a in b     2.7
assertNotIn(a, b)     a not in b     2.7
assertIsInstance(a, b)     isinstance(a, b)     2.7
assertNotIsInstance(a, b)     not isinstance(a, b)     2.7

3.测试用例

#coding:utf-8
import unittest
import time
import random

import HTMLTestRunner
 
class Test_Class(unittest.TestCase):
      
    def setUp(self):
        self.seq = range(10)
 
    def test_shuffle(self):  
        # make sure the shuffled sequence does not lose any elements  
        random.shuffle(self.seq)  
        self.seq.sort()  
        self.assertEqual(self.seq, range(10))  
          
        # should raise an exception for an immutable sequence  
        print("test_shuffle")
        self.assertRaises(TypeError, random.shuffle, (1,2,3))  
  
    def test_choice(self):  
        element = random.choice(self.seq)  
        print("test_choice")
        self.assertTrue(element in self.seq)  
   
    def test_sample(self):  
        with self.assertRaises(ValueError):  
            random.sample(self.seq, 20)  
        for element in random.sample(self.seq, 5):  
            self.assertTrue(element in self.seq)

    def sun(self):
        self.temp = 5 + 6
        print("sun test")
        self.assertEqual(self.temp,11)
 
                  
if __name__ == "__main__":
     
    testsuite = unittest.TestSuite()   
     
#    添加测试用例到测试集中
    testsuite.addTest(Test_Class("test_shuffle"))
    testsuite.addTest(Test_Class("test_choice"))
    testsuite.addTest(Test_Class("test_sample"))
    testsuite.addTest(Test_Class("sun"))
         
#   生成测试报告文件
    filename = 'D:\result.html'
    fp = file(filename, 'wb')
     
    runner = HTMLTestRunner.HTMLTestRunner(
                stream=fp,
                title='测试结果',
                description='测试报告.'
                )
#    runner = unittest.TextTestRunner()
    runner.run(testsuite)

  运行后测试结果会生成为D:\result.html 的文件,当然测试结果的文件名称你可以随意命名,或者根据时间自动生成。直接打开后就可以看到测试结果。

三、测试报告

  

四、使用说明

1.test_case中print信息出现在report中,运行批处理窗口没有打印信息。

2.Python shell中运行同样如此

但还有一个问题,Python shell中运行时未产生测试报告,测试报告文件大小为0

原文地址:https://www.cnblogs.com/hester/p/4703054.html