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()
相关推荐
IT_Octopus4 小时前
【零基础入门 LLM 开发 · Day 13】:LangChain 的对话记忆——Message 对象版
人工智能·python·langchain
Jmyd01234 小时前
Web端3D营销展厅技术实现路径:从素材上云到线索回流的数据链路
前端·3d·性能优化·三维数字化
砚凝霜5 小时前
【软考信息安全】第二十二章 Web服务器安全分析与增强——Apache与IIS
服务器·前端·安全
2603_965148115 小时前
社区与学习资源:从入门到精通的API选品学习路径
数据库·人工智能·python·学习·数据挖掘
CCblandy6 小时前
令牌桶在 0.9 倍负载超发 19%、固定窗口边界放行 20 个:四种限流算法同构实测
后端·python·算法·性能优化·架构
建筑工程企业管理系统6 小时前
工程进度统计管理系统自动化汇总数据,解决施工企业手工对账误差难题
大数据·人工智能·自动化
u0111026757 小时前
JPG、PNG、WebP 怎么选?按透明度、体积和使用场景做判断
前端·javascript·vue.js·图像处理·ai作画
Cc.Y7 小时前
Java零基础入门:深入Java核心:文件IO与序列化全解
java·开发语言·python
拿本唠嗑AI研究7 小时前
本体工程实战指南:从建模到可维护的完整落地路径
java·人工智能·python·jena·ontology
jerrywus7 小时前
让 AI 写完代码后交一份报告:我改造了 answer-me-with-html
前端·claude·cursor