欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python自动化报告的输出用例详解

程序员文章站 2022-07-22 18:31:01
1、设计简单的用例 2、设计用例    以testbaidulinks.py命名 # coding:utf-8 from selenium import...

1、设计简单的用例

python自动化报告的输出用例详解

2、设计用例

   以testbaidulinks.py命名

# coding:utf-8
from selenium import webdriver
import unittest
class baidulinks(unittest.testcase):
 def setup(self):
  base_url = 'https://www.baidu.com'
  self.driver = webdriver.chrome()
  self.driver.implicitly_wait(10)
  self.driver.get(base_url)
 def teardown(self):
  self.driver.close()
  self.driver.quit()
 def test_baidu_news(self):
  u"""百度新闻"""
  driver = self.driver
  driver.find_element_by_link_text('新闻').click()
  self.assertin(driver.title, u'v百度新闻——全球最大的中文新闻平台')
 def test_baidu_hao123(self):
  u"""hao123"""
  driver = self.driver
  driver.find_element_by_link_text('hao123').click()
  self.assertequal(driver.title, u'hao123_上网从这里开始')
 def test_baidu_tieba(self):
  u"""百度贴吧"""
  driver = self.driver
  driver.find_element_by_link_text('贴吧').click()
  # 错误的断言
  self.asserttrue(driver.find_element_by_link_text('全吧搜索+1'))
if __name__ == '__main__':
 unittest.main()
 print('百度链接跳转成功')

3、写执行用例脚本

# /usr/bin/env python3
# coding:utf-8
"""
created on 2018-05-30
project: learning
@author:wuya
"""
import os, time, unittest
import htmltestrunner
report_path = os.getcwd() # 设置保存报告的路径,这儿设置的是与执行文件在同一个目录下
now = time.strftime("%y-%m-%d %h:%m", time.localtime(time.time())) # 获取当前时间
title = u"百度头链接测试" # 标题
report_abspath = os.path.join(report_path, title + now + ".html") # 设置报告存放和命名
# 导入用例
def all_case():
 case_path = os.getcwd() # 用例路径,这儿的用例和执行文件在同一目录下
 discover = unittest.defaulttestloader.discover(case_path,             pattern="test*.py") # 添加用例,在case_path的路径下,所有以test开头的文件都当做用例文件执行
 print(discover)
 return discover
if __name__ == "__main__":
 fp = open(report_abspath, "wb") # 保存报告文件
 runner = htmltestrunner.htmltestrunner(stream=fp,           title=title + ':',)
 runner.run(all_case()) # 执行用例
 fp.close()

4、执行结果

  结果文档下载

python自动化报告的输出用例详解

  对于执行不通过的用例可以点击错误二字查看详情

python自动化报告的输出用例详解

总结

以上所述是小编给大家介绍的python自动化报告的输出用例详解,希望对大家有所帮助