zoukankan      html  css  js  c++  java
  • ui自动化收尾,出测试报告,用例失败成重跑,加日志发钉钉讲解

    主要学习 1.测试报告,2.用例失败重跑,3.测试结果,邮件,群聊天工具

     安装步骤

    工具名称allure 额老恩
    1.解压文件
    2.将解压bin 目录放到系统环境变量path 下
    3.allure --version cmd输入
    4.python 安装 pip install allure-pytest

     面试问题一?出报告的流程是啥???

    第一步:我们装的是本地化的工具

    第二步:我们用python装的是和他继承的插件pip install allure-pytest

    第三步:pytest -s -q --alluredir report/allure/path  执行命令把出测试报告路径出出来

    第四步:allure serve reportallure  使用allure命令执行生产测试报告

    allure generate [json_report_path] -o [html_report_path]

    关于 allure权限问题:
    import sys
    sys.path.append('/xx/xx/')

    allure报告内容展示
    allure用例描述
    使用方法 参数值 参数说明
    @allure.epic() epic描述 敏捷里面的概念,定义史诗,往下是feature
    @allure.feature() 模块名称 功能点的描述,往下是story
    @allure.story() 用户故事 用户故事,往下是title
    @allure.title(用例的标题) 用例的标题 重命名html报告名称
    @allure.testcase() 测试用例的链接地址 对应功能测试用例系统里面的case
    @allure.issue() 缺陷 对应缺陷管理系统里面的链接
    @allure.description() 用例描述 测试用例的描述
    @allure.step() 操作步骤 测试用例的步骤
    @allure.severity() 用例等级 blocker,critical,normal,minor,trivial
    @allure.link() 链接 定义一个链接,在测试报告展现
    @allure.attachment() 附件 报告添加附件

     例子文件

    
    
    from pages.LoginPage import LoginPage
    from selenium import webdriver
    import allure
    @allure.story('登录')
    @allure.title('正向登录')
    @allure.description('11111111')
    @allure.issue('from pages.LoginPage import LoginPage
    from selenium import webdriver
    import allure
    @allure.story('登录')
    @allure.title('正向登录')
    @allure.description('11111111')
    def test_login01(locator, check):
    locator.点击("首页","登录链接")
    locator.输入("登录","用户名", "admin@admin.com")
    locator.输入("登录","密码", "111111")
    locator.点击("登录","按钮")
    check.相等(locator.获取文本值("首页","安全退出"), "安全退出", '登录检查成功')
    @allure.story('登录')
    @allure.title('密码错误')
    @allure.description('2222222')
    @allure.testcase('https://www.baidu.com/?tn=94819464_hao_pg')#对应bug地址
    def test_login01(locator, check):
    locator.点击("首页","登录链接")
    locator.输入("登录","用户名", "admin@admin.com")
    locator.输入("登录","密码", "111111")
    locator.点击("登录","按钮")
    check.相等(locator.获取文本值("首页","安全退出"), "安全退出", '登录检查成功')
    def test_login01():
    driver = webdriver.Chrome()
    LoginPage(driver).login()
    ')
    def test_login01(locator, check):
    locator.点击("首页","登录链接")
    locator.输入("登录","用户名", "admin@admin.com")
    locator.输入("登录","密码", "111111")
    locator.点击("登录","按钮")
    check.相等(locator.获取文本值("首页","安全退出"), "安全退出", '登录检查成功')
    @allure.story('登录')
    @allure.title('密码错误')
    @allure.description('2222222')
    @allure.testcase('https://www.baidu.com/?tn=94819464_hao_pg')#对应功能测试地址
    def test_login01(locator, check):
    locator.点击("首页","登录链接")
    locator.输入("登录","用户名", "admin@admin.com")
    locator.输入("登录","密码", "111111")
    locator.点击("登录","按钮")
    check.相等(locator.获取文本值("首页","安全退出"), "安全退出", '登录检查成功')
    def test_login01():
    driver = webdriver.Chrome()
    LoginPage(driver).login()

    python执行cmd命令方法 os模块?????????????????
    cmd = 'ping 192.168.1.1 '
    res = os.popen(cmd)
    output_str = res.read() # 获得输出字符串
    print(output_str)

    在run入口执行产生报告代码???
    import pytest
    import time
    import subprocess


    if __name__ == '__main__':
    tmp = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
    pytest.main(['-s', './cases/test_login01.py', f'--alluredir=report/{tmp}/json/'])
    subprocess.call(f'allure generate report/{tmp}/json/ -o report/{tmp}/html/', shell=True)
    #这行意思等待用例执行完了生成报告会等待

    想要展示日志信息怎么搞???????????? 在core下文件locator
    from selenium import webdriver
    import time
    import csv
    import allure
    from core import config

    class Locator:

    def __init__(self):
    self.driver = webdriver.Chrome()
    config.DRIVER = self.driver

    def element(self, page, name):
    with open('./对象库.csv', 'r', encoding='utf-8') as f:
    for i in csv.reader(f):
    if page == i[0] and name == i[1]:
    key = i[2]
    value = i[3]
    break
    else:
    raise Exception(f"对象未在对象库定义:{page} {name}")
    if key == 'id':
    try:
    return self.driver.find_element_by_id(value)
    except:
    raise Exception(f"未找到指定元素,定位信息:by {key}, {value}")
    elif key == 'name':
    try:
    return self.driver.find_element_by_name(value)
    except:
    raise Exception(f"未找到指定元素,定位信息:by {key}, {value}")
    elif key == 'class':
    try:
    return self.driver.find_element_by_class_name(value)
    except:
    raise Exception(f"未找到指定元素,定位信息:by {key}, {value}")
    elif key == 'link_text':
    try:
    return self.driver.find_element_by_link_text(value)
    except:
    raise Exception(f"未找到指定元素,定位信息:by {key}, {value}")
    elif key == 'xpath':
    try:
    return self.driver.find_element_by_xpath(value)
    except:
    raise Exception(f"未找到指定元素,定位信息:by {key}, {value}")

    def elements(self, key, value):
    if key == 'xpath':
    return self.driver.find_elements_by_xpath(value)
    elif key == 'class':
    return self.driver.find_elements_by_class_name(value)

    @allure.step("点击元素")
    def 点击(self, page, name):
    self.element(page, name).click()

    @allure.step("输入内容")
    def 输入(self, page, name, text):
    self.element(page, name).send_keys(text)

    def 跳转(self, url):
    self.driver.get(url)

    def 等待(self, s):
    time.sleep(s)

    def 获取文本值(self, key, value):
    return self.element(key, value).text

    def 获取多个文本值(self, key, value):
    texts = []
    element_list = self.elements(key, value)
    for e in element_list:
    texts.append(e.text)
    return texts

    def 获取价格(self, key, value):
    texts = []
    element_list = self.elements(key, value)
    for e in element_list:
    texts.append(int(e.text[1:-3]))
    return texts

    @property
    def 关闭(self):
    self.driver.close()

    @property
    def 窗口最大化(self):
    self.driver.maximize_window()

    def 全局等待(self, s):
    self.driver.implicitly_wait(s)

    def log(self, content):
    allure.attach(content, '日志', allure.attachment_type.TEXT) 这句话是打印日志的
    怎么调用日志用例?????????????????
    夹具函数加代码
    @pytest.fixture()
    def check():
    return Checkpoint() 自动引入断言模块
    def test_login01(locator, check):
    locator.点击("首页","登录链接")
    locator.输入("登录","用户名", "admin@admin.com")
    locator.输入("登录","密码", "111111")
    locator.点击("登录","按钮")
    check.相等(locator.获取文本值("首页", "安全退出"), "安全退出2", '登录检查成功')
    check.result() 导入断言校验
    失败的日志呢,反向的????????
    先在 locator封装元素里加打印日志方法,然后在夹具函数conftest里面导入封装元素模块
    from core.Locator import Locator  这时候就可以在用例脚本直接使用夹具方法直接调用了
     def log(self, content):
    allure.attach(content, '日志', allure.attachment_type.TEXT)

     如何测试报告里面带截图????????????check里面加这句话

    image=config.DRIVER.get_screenshot_as_base64()
    allure.attach(f'<img src="data:image/png;base64,{image}"/>', '屏幕截图', allure.attachment_type.HTML)

    失败用例重跑?????????

     失败重跑

    pip3 install pytest-rerunfailures==9.0

    一定要安装9.0版本,否则不可以和fixture装饰器一起使用: @pytest.fixture()

    方式一:@pytest.mark.flaky(reruns=5, reruns_delay=2)

    方式二:要重新运行所有测试失败,使用 --reruns 命令行选项,并指定要运行测试的最大次数:

    实例?????????

    pytest.main(['-s', './cases/test_login01.py', f'--alluredir=report/{tmp}/json/','--reruns=3','--reruns-delay=2'])
    #所有用例失败重跑三次,间隔2s
    https://www.jianshu.com/p/6a0fadc6c66f 失败重跑报log错误解决办法 
    不想失败重跑直接拿到,如果控制每一个用例就加标签

    总结下框架的目录????????
    run 整体执行
    csv对象库管理所有操作对象定位信息
    case 管理所有用例
    core 核心源码
    包含检查点-封装断言,失败有截图 断言有步骤 ,还有放一个driver入口全局配置文件
    locator 核心对seliuem原本函数封装 夹具函数
    case下还有夹具函数
    用例里面引入两个函数
    第一个函数locator是操作函数
    第二check是检查点的函数
    最后check.result收尾函数






     
     
  • 相关阅读:
    简记配置tesorflow-gpu环境(吐血了)
    个人总结
    浪潮之巅阅读笔记03
    第十六周的学习进度条
    第十五周的学习进度条
    第十四的周学习进度条
    课堂练习-购书问题
    第二阶段—个人工作总结10
    第二阶段—个人工作总结09
    第二阶段—个人工作总结08
  • 原文地址:https://www.cnblogs.com/weilemeizi/p/15367635.html
Copyright © 2011-2022 走看看