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()
相关推荐
满怀10154 分钟前
【人工智能核心技术全景解读】从机器学习到深度学习实战
人工智能·python·深度学习·机器学习·tensorflow
乐言3619 分钟前
Jmeter中的BeanShell如何使用?
python·jmeter·压力测试
水银嘻嘻13 分钟前
web 自动化之 selenium 下拉&鼠标键盘&文件上传
selenium·自动化
航Hang*16 分钟前
前端项目2-01:个人简介页面
前端·经验分享·html·css3·html5·webstorm
深圳安锐科技有限公司19 分钟前
高速边坡监测成本高?自动化如何用精准数据省预算?
运维·自动化
MaisieKim_29 分钟前
python与nodejs哪个性能高
前端·python·node.js
林鸿风采1 小时前
内网服务器之间传输单个大文件最佳解决方案
linux·python·文件传输
森叶1 小时前
从 JIT 即时编译一直讲到CGI|FastGGI|WSGI|ASGI四种协议的实现细节
python·php·web
水煮白菜王1 小时前
深入理解 Webpack 核心机制与编译流程
前端·webpack·node.js
weixin_307779131 小时前
使用FastAPI微服务在AWS EKS中构建上下文增强型AI问答系统
人工智能·python·云计算·fastapi·aws