• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    allure结合python生成测试报告教程

    百度搜索实例

    一、代码结构

    本案例来自于霍格沃兹测试学院《高薪测试成长图谱》。data.yml为数据管理文件,test_baidudemo.py为测试用例文件,文件结构如下:

    创建data/data.yml文件,代码如下

    - allure
    - pytest
    - unittest
    

    创建test_baidudemo.py,代码如下

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    import allure
    import pytest
    import yaml
    from selenium import webdriver
    import time
    @allure.testcase("https://www.baidu.com")
    @allure.feature("百度搜索")
    @pytest.mark.parametrize('test_data1',yaml.safe_load(open("data/data.yml")))
    def test_steps_demo(test_data1):
        with allure.step("打开百度网页"):
            driver = webdriver.Chrome()
            driver.get("https://www.baidu.com")
            driver.maximize_window()
        with allure.step(f"输入搜索词:{test_data1}"):
            driver.find_element_by_id("kw").send_keys(test_data1)
            time.sleep(2)
            driver.find_element_by_id("su").click()
            time.sleep(2)
        with allure.step("保存图片"):
            driver.save_screenshot("./result/b.png")
            allure.attach.file("./result/b.png",attachment_type=allure.attachment_type.PNG)
        with allure.step("关闭浏览器"):
            driver.quit()
    

    二、运行结果

    进入项目目录下,使用以下语句运行

    pytest test_baidudemo.py -s -q --alluredir=./result/    #执行测试用例,并生成测试报告数据在当前文件夹result文件下
    allure serve ./result/     #启动allure,并使用result下的测试结果数据生成测试报告

    生成的报告如下图所示:

    问题解决

    运行时总是报错当前chromedriver只支持chrome78,实际上已经更新了chromedriver83,未找到原因解决,最终在代码里加上chromedriver绝对路径。将driver = webdriver.Chrome()修改为driver = webdriver.Chrome(‘C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')。

    with allure.step(f"输入搜索词:{test_data1}"):,在python3.6.8版本上运行该语句总是报语法错误,修改为 with allure.step(“输入搜索词:”+test_data1):,可以正常运行并输出。

    allure简介与使用

    allure简介

    Allure是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。

    allure如何生成测试报告

    运行的时候加上 pytest.main ( ‘–alluredir', ‘report/result', ‘TestDemo01.py']) 会在当前文件夹创建一个report文件夹,在report文件夹下创建result

    生成html测试报告

    因为生成的测试报告是json的,不好看,所有用这个命令生成一个好看的HTML测试报告

    运行之后,就会生成一个HTML文件夹,点开index.html这个就是我们的测试报告啦

    allure几个常用特性(测试报告中展示)

    @allure.feature (用于描述被测试产品需求)

    @allure.story (用于描述feature的用户场景,即测试需求)

    with allure.step() (用于描述测试步骤,将会输出到报告中)

    allure.attach (用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图等)

    pytest断言设置并结合allure生成测试报告

    测试报告

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    您可能感兴趣的文章:
    • python自动化之如何利用allure生成测试报告
    • 详解用Pytest+Allure生成漂亮的HTML图形化测试报告
    • Pytest allure 命令行参数的使用
    上一篇:Python爬取哆啦A梦-伴我同行2豆瓣影评并生成词云图
    下一篇:Python模拟登录微博并爬取表情包
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    allure结合python生成测试报告教程 allure,结合,python,生成,测试,