web 自动化之 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)
        # 断言  获取账号的名称=username
        el_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()
相关推荐
用户69371750013844 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦4 小时前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013844 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
qq_417695055 小时前
机器学习与人工智能
jvm·数据库·python
漫随流水5 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
yy我不解释6 小时前
关于comfyui的mmaudio音频生成插件时时间不一致问题(一)
python·ai作画·音视频·comfyui
踩着两条虫6 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
紫丁香7 小时前
AutoGen详解一
后端·python·flask
FreakStudio7 小时前
不用费劲编译ulab了!纯Mpy矩阵micronumpy库,单片机直接跑
python·嵌入式·边缘计算·电子diy
jzlhll1237 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin