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()
相关推荐
SoaringHeart6 分钟前
Flutter组件封装:标签拖拽排序 NDragSortWrap
前端·flutter
zeijiershuai8 分钟前
Vue 工程化、ElementPlus 快速入门、ElementPlus 常见组件-表格组件、ElementPlus常见组件-分页条组件
前端·javascript·vue.js
程序员爱钓鱼15 分钟前
Python 编程实战 · 进阶与职业发展:数据分析与 AI(Pandas、NumPy、Scikit-learn)
后端·python·trae
软件开发技术深度爱好者19 分钟前
Python库/包/模块管理工具
开发语言·python
程序员爱钓鱼27 分钟前
Python 编程实战 · 进阶与职业发展:Web 全栈(Django / FastAPI)
后端·python·trae
漫天黄叶远飞28 分钟前
把原型链画成地铁图:坐 3 站路就能看懂 JS 的“继承”怎么跑
前端·javascript
bank_dreamer31 分钟前
VSCODE前端代码风格格式化
前端·css·vscode·html·js·prettier·代码格式化
IT_陈寒1 小时前
90%的Python开发者不知道:这5个内置函数让你的代码效率提升300%
前端·人工智能·后端
网络点点滴1 小时前
Vue3的生命周期
前端·javascript·vue.js