Python3+unittest+HTMLTestRunner生成自动化测试报告

本文介绍了如何在Python3环境中结合unittest和HTMLTestRunner编写自动化测试用例并生成测试报告。首先,展示了项目文件结构,接着详细讲解了HTMLTestRunner的安装与用法,包括在Python3中进行的必要修改。然后,文章列举并解决了在安装unittest过程中遇到的问题,以及在运行测试报告时出现的AttributeError。通过文中提供的步骤,读者可以成功生成自动化测试报告。
摘要由CSDN通过智能技术生成

项目地址:https://gitee.com/jtwen/item_test

1 文件结构

  1. 在 pycharm 中新建一个项目 item_test,结构如下
    在这里插入图片描述

  2. result 为测试结果文件夹(随后需在此文件夹里生成测试报告)

  3. testcase 为测试用例文件夹

  4. exe.py 此处为执行文件(可执行它来生成测试报告)

2 编写用例

  1. 在testcase文件夹中新建 testcase_jd.py

# 此处以京东网址为例,为了保证通过率只设置两个简单的用例(实际情况会略有不同)
import time
import unittest

from selenium import webdriver


class testcase_case(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.jd.com")
        time.sleep(3)  # 显示等待3秒

    def test_search(self):  # 搜索查找
        driver = self.driver
        driver.find_element_by_id("key").send_keys("iphone12")  # 通过 id 定位
        driver.find_element_by_css_selector("div.form>button.button").click()  # 通过 css 选择器定位
        title = "京东"
        self.assertIn(title, driver.title)  # 设置断言,判断网页名称是否包含title,包含就算通过

    def test_miaosha(self):  # 秒杀
        driver = self.driver
        driver.find_element_by_link_text("秒杀").click()  # 通过链接的文字定位
        print(driver.title)
        title2 = "京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!"
        self.assertEqual(title2, driver.title)  # 采用强匹配,只有相等才算通过用例
        time.sleep(3)
        driver.close()  # 2条用例执行完毕

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()
  1. HtmlTestRunner安装

    a. 打开此网址: http://tungwaiyip.info/software/HTMLTestRunner.html,找到 HTMLTestRunner.py 右键,另存为;
    b. 此文件适用 python2,在python3中需手动修改:

94行 import io
539  self.outputBuffer = io.StringO()
642  if not cls in rmap
766  uo = e
772  ue = e
631  print(sys.stderr,'\nTime Elapsed:%s'%(self.stopTime-self.startTime))

修改之后文件如下:

"""
A TestRunner for use with the Python unit testing framework. It
generates a HTML report to show the result at a glance.

The simplest way to use this is to invoke its main method. E.g.

   import unittest
   import HTMLTestRunner

   ... define your tests ...

   if __name__ == '__main__':
       HTMLTestRunner.main()


For more customization options, instantiates a HTMLTestRunner object.
HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g.

   # output to a file
   fp = file('my_report.html', 'wb')
   runner = HTMLTestRunner.HTMLTestRunner(
               stream=fp,
               title='My unit test',
               description='This demonstrates the report output by HTMLTestRunner.'
               )

   # Use an external stylesheet.
   # See the Template_mixin class for more customizable options
   runner.STYLESHEET_TMPL = '<link rel="stylesheet" href="my_stylesheet.css" type="text/css">'

   # run the test
   runner.run(my_test_suite)


------------------------------------------------------------------------
Copyright (c) 2004-2007, Wai Yip Tung
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the f
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值