当前位置: 首页 > news >正文

web 自动化之 Unittest 应用:测试报告装饰器断言

文章目录

      • 一、常见的第三方库结合 unittest 生产 html 格式测试报告
        • 1、HtmlTestRunner
        • 2、BeatifulReport
      • 二、装饰器 @ unittest.skip 强制跳过&条件跳过
      • 三、unittest的常用断言方法

一、常见的第三方库结合 unittest 生产 html 格式测试报告

1、HtmlTestRunner

官网下载 HtmlTestRunner.py 只能支持 python2 版本,支持 Python3 ,需要做修改
路径:python安装路径/Lib

import unittest
from TestReport.testcase_01 import TestCase01
import HTMLTestRunner# 加载用例
testcases = unittest.TestLoader().loadTestsFromTestCase(TestCase01)
# HTMLTestRunner 生成的测试报告
with open('F:/Pycharm/TestShop/TestReport/html_report.html', 'wb+') as hf:HTMLTestRunner.HTMLTestRunner(stream=hf,title="Html 测试报告",description="测试用例执行详情").run(testcases)

HTMLTestRunner 文件内容

2、BeatifulReport

安装三方库:BeatifulReport

import unittest
from TestReport.testcase_01 import TestCase01
from BeautifulReport import BeautifulReport# 加载用例
testcases = unittest.TestLoader().loadTestsFromTestCase(TestCase01)
# BeautifulReport 生成的测试报告
BeautifulReport(testcases).report(description="Html 测试报告",filename="report_bf",report_dir="reports")

企业测试报告的优化及定制 优化测试报告模板 通过 js+html/html5
pytest+allure 生成更加美观的测试报告+优化定制(装饰器)

二、装饰器 @ unittest.skip 强制跳过&条件跳过

import unittest
"""
@unittest.skip 强制跳过执行
@unittest.skipIf 符合条件,则跳过执行
@unittest.skipUnless 条件不成立,则跳过执行
"""
# @unittest.skipUnless(False,"装饰器也可以作用于类,整个模块下的用例强制跳过执行")
class TestCase01(unittest.TestCase):@unittest.skip("此用例暂时不启用")def test_login(self):print('test_01')@unittest.skipIf(True,"符合条件,则跳过执行")def test_select_goods(self):print('test_02')@unittest.skipUnless(2>3, "不符合条件,则跳过执行")def test_put_to_cart(self):print('test_03')def test_pay_goods(self):print('test_04')
if __name__ == '__main__':unittest.main()

三、unittest的常用断言方法

import unittest
from selenium import webdriver
import time
from selenium.webdriver.common.by import By'''
二、常用断言方法
1、assertIn(字符1,字符2) 字符1是否包含在字符2
2、self.assertNotIn(字符1,字符2)  字符1不包含包含在字符2
self.assertEqual(参数1,参数2,"断言失败的描述")  参数1等于参数2
self.assertNotEqual(参数1,参数2,"断言失败的描述")参数不等于参数2
self.assertTrue(True)
self.assertFalse(False)
'''
class TestCase02(unittest.TestCase):def setUp(self) -> None:# 打开浏览器self.driver = webdriver.Edge()# 加载地址self.driver.get("http://116.62.63.211/shop/")def test_login(self):"""登录用例"""username = "hc_test"password = "hctest123"el_login_link = self.driver.find_element(By.LINK_TEXT, "登录")# 点击登录el_login_link.click()time.sleep(1)el_login = self.driver.find_element(By.XPATH, '//button[text()="登录"]')el_username = self.driver.find_element(By.NAME, "accounts")el_password = self.driver.find_element(By.XPATH, '//input[@type="password"]')# 输入用户名el_username.send_keys(username)# 输入密码el_password.send_keys(password)# 点击登录time.sleep(1)el_login.click()time.sleep(2)# 断言  获取账号的名称=usernameel_username = self.driver.find_element(By.XPATH, "//div[@class='menu-hd']/em[2]")self.assertIn(username, el_username.text)def test_select_goods(self):"""检索商品"""self.driver.get("http://116.62.63.211/shop/")select_goods = "手机"# 定位搜索输入el_select = self.driver.find_element(By.ID, "search-input")el_select.send_keys(select_goods)el_button = self.driver.find_element(By.ID, "ai-topsearch")el_button.click()time.sleep(2)# 断言:验证测试结果与预期结果是否一致# 获取商品列表的标题el_goods = self.driver.find_elements(By.XPATH, "//div[@class='items am-padding-bottom-xs']")# 判断content是否包含手机字符?# 标题是否包含手机for el in el_goods:self.assertIn(select_goods, el.text, "商品标题中未包含检索内容")def tearDown(self) -> None:self.driver.close()if __name__ == '__main__':unittest.main()
http://www.xdnf.cn/news/413263.html

相关文章:

  • 基于大模型研究技术方案清单
  • 【markdown】介绍如何在markdown中绘制流程图
  • 在嵌入式调试中IAR提示Fatal error: CPU did not power up Session aborted!怎么回事?怎么解决?
  • C++中类中const知识应用详解
  • PyCharm历史版本下载说明
  • Java大师成长计划之第20天:Spring Framework基础
  • Qt/C++面试【速通笔记九】—视图框架机制
  • EHS 安全管理有效落地,五步实施方法解析
  • 基于SpringBoot的博客系统测试报告
  • slackel系统详解
  • MACH-ETH:汽车网络接口的卓越之选
  • steam OS详细讲解
  • yolov5s.pt这类的后续是pt的文件用什么软件可以打开看
  • STM32F103_LL库+寄存器学习笔记12.1 - 串口DMA高效收发实战:引入ringbuffer结构
  • STM32实现循环队列
  • 系统架构-通信系统架构设计
  • 如何理解“数组也是对象“——Java中的数组
  • old kali网站下载链接爬取-Kali linux 全部版本镜像下载--Index of /kali-images
  • 基于STM32、HAL库的DPS368XTSA1气压传感器 驱动程序设计
  • [Windows] Honeyview V5.53
  • 深度解析Crawl4AI:面向大模型的新一代智能爬虫
  • 2025系统架构师考试押题总结
  • 学习黑客Windows 任务管理器详解
  • 解决WSL、Ubuntu的.ico图标不正确显示缩略图
  • VBA会被Python代替吗
  • LeetCode:513、找树左下角的值
  • 滑动窗口/单调队列
  • [网络层]ICMP协议
  • Java——API基础(String类和StringBuilder类)
  • 手写 vue 源码 === computed 实现