51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1132|回复: 0
打印 上一主题 下一主题

Python+Selenium+UnitTest测试报告如何写?

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:14
  • 签到天数: 938 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-11-18 14:24:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
     1、创建test.py文件
    import unittest
      #创建测试类Red,首字母大写,括号内表示继承不能省略
      class Red(unittest.TestCase):
      #初始化函数,所有测试函数运行前运行一次,@classmethod为装饰器,cls不能省略
          @classmethod
          def setUpClass(cls):
              print("starting....")
      #每个测试函数运行前运行,可运行多次
          def setUp(self):
              print("test function starting")
      #每个测试函数运行完运行,可运行多次
          def tearDown(self):
              print("test function ending")
      #创建测试函数testA,执行顺序
          def testopenweb1(self):
              print("test A")
      #创建测试函数testB
          def testopenweb2(self):
              print("test B")
      #所有测试函数运行完运行一次
          @classmethod
          def tearDownClass(cls):
              print("ending....")
      #单独运行模块时才会被执行
      if __name__=='__main__':
          unittest.main()



     2、HTMLTestRunner.py模板配置
      网上download一个HTMLTestRunner.py模板,放入python\lib目录下。
      3、创建report.py文件
    #运行测试套件下得所有函数
      import  unittest
      from test import Red
      import time
      import HTMLTestRunner
      #获取Red类下得所有测试方法
      test=unittest.TestLoader().loadTestsFromTestCase(Red)
      #创建一个测试套件
      suite=unittest.TestSuite()
      tests=[Red("testopenweb1"),Red("testopenweb2")]
      suite.addTests(tests)
      #定义个时间字符串
      now_time=time.strftime("%Y-%m-%d %H.%M.%S")
      #指定测试报告名称(存放目录必须存在)
      file_path = "D:\\report\\" + now_time + " report.html"
      #以二进制的形式写入文件w="write",b="binary",纯文本文件不需二进制格式
      file=open(file_path,"wb")
      runner=HTMLTestRunner.HTMLTestRunner(stream=file,title="XX项目web测试报告",description="用例执行情况如下",tester="ceshi")
      runner.run(suite)
      #关闭文件
      file.close()



    4、报告查看
      执行后,对应的目录查看测试报告,如图所示:




    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-4-27 10:16 , Processed in 0.062969 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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