自动化基础中,如何详细解析allure用例描述并实战应用?

2026-05-27 11:501阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1368个文字,预计阅读时间需要6分钟。

自动化基础中,如何详细解析allure用例描述并实战应用?

前言 + allure 可以为pytest提供高质量的测试报告,并且可以与pytest完美结合。不仅支持页面渲染,还可以控制用例的执行。以下将对allure的使用进行详细介绍和总结。+ 需要准备的环境:

自动化基础中,如何详细解析allure用例描述并实战应用?

+ 系统环境:Python 3.6及以上版本+ 测试框架:pytest+ allure:安装allure-pytest插件

前言

allure可以输出非常精美的测试报告,也可以和pytest进行完美结合,不仅可以渲染页面,还可以控制用例的执行。下面就对allure的使用进行一个详细的介绍和总结。

需要准备的环境:

  • python
  • pytest
  • allure-pytest
  • allure工具
一、allure用例描述 使用方法 参数值 参数说明 @allure.epic() epic描述 敏捷里面的概念,对用例或用例集进行描述分类 @allure.feature() 模块名称 与epic类似,只是比epic级别低 @allure.story() 用户故事 与epic类似,只是比feature级别低 @allure.title(用例的标题) 用例的标题 重命名html报告的用例名称 @allure.testcase() 测试用例的链接地址 与link类似 @allure.issue() 缺陷 与link类似 @allure.description() 用例描述 进行测试用例的描述 @allure.step() 操作步骤 进行测试用例的步骤 @allure.severity() 用例等级 blocker,critical,normal,minor,trivial @allure.link() 链接 定义一个链接,在测试报告展现(推荐使用) @allure.attachment() 附件 报告添加附件 二、allure实战demo

# -*- coding:utf-8 -*- """ @File: allure_demo.py @Author: 三叔测试笔记 @Time : 2022/3/18 8:34 @Description: allure使用介绍 """ import pytest import allure from base.log import Logger logger = Logger(logger_name='allure', level='error').get_logger() @pytest.fixture(scope="session") # 用例前置操作 def login_fixture(): # 比如登录获取token操作 return "token:xx" @allure.step("用例步骤1") def step_1(): logger.info("用例操作---------------步骤1") return True @allure.step("用例步骤2") def step_2(): logger.info("用例操作---------------步骤2") return False @allure.step("用例步骤3") def step_3(): logger.info("用例操作---------------步骤3") return True @allure.epic("可以对用例或用例集进行描述分类(若出现多个时,内容一致则自动归为一类)") @allure.feature("对用例集或用例进行描述分类---与epic类似,只是比epic级别低") @allure.story("对用例集或用例进行描述分类---与epic类似,只是比feature级别低") class TestAllureDemo: @allure.testcase("xxx/testcase/list", name='用例链接testcase') # 为了更好的链接到问题分类或者bug、测试用例地址中(url、name两个参数,可不填写name;可以用@allure.link) @allure.link("xxx/testcase/list", name='用例链接link') # 与testcase没有多大区别,从可读性角度还是建议选择@allure.link @allure.issue("xxx/testcase/list", name='用例链接issue') # 与testcase区别在于有小虫子图标 @allure.title("用例的标题") # 可参数化标题 @allure.story("用例分类:1") # 可参数化标题 @allure.severity("critical") # 用例等级(blocker critical normal minor trivial) def test_case_1(self, login_fixture): """ 1.用例描述 2.用例步骤 3.预期结果 """ logger.info(login_fixture) # 获取用例前置的信息,比如登录token assert step_1() assert step_2() @allure.story("用例分类:2") def test_case_2(self, login_fixture): logger.info("测试用例2") assert step_1() assert step_3() @allure.epic("冒烟自动化用例") class TestDemo2: @allure.story("用例分类:3") def test_case_3(self, login_fixture): logger.info("测试用例3") step_1() @allure.story("用例分类:4") def test_case_4(self, login_fixture): logger.info("测试用例4") step_3() 三、allure的命令行参数

pytest执行用例时可以加上allure的标记参数,可以控制执行哪些用例。

--allure-severities=SEVERITIES_SET Comma-separated list of severity names. Tests only with these severities will be run. Possible values are: blocker, critical, normal, minor, trivial. --allure-epics=EPICS_SET Comma-separated list of epic names. Run tests that have at least one of the specified feature labels. --allure-features=FEATURES_SET Comma-separated list of feature names. Run tests that have at least one of the specified feature labels. --allure-stories=STORIES_SET Comma-separated list of story names. Run tests that have at least one of the specified story labels. --allure-link-pattern=LINK_TYPE:LINK_PATTERN Url pattern for link type. Allows short links in test, like 'issue-1'. Text will be formatted to full url with python str.format(). 实例如下:

# 选择运行你要执行epic的用例 pytest --alluredir ./report/allure --allure-epics=epic对大Story的一个描述性标签 # 选择运行你要执行features的用例 pytest --alluredir ./report/allure --allure-features=模块2 # 选择运行你要执行features的用例 pytest --alluredir ./report/allure --allure-stories="用户故事:1" 四、执行脚本,allure生成报表,并启动报告 运行方式一:

  1. 命令行模式下运行pytest,生产测试结果文件
    pytest demo.py --alluredir ./report/allure

  2. allure程序启动已经生产的文件
    allure serve ./report/allure

运行方式二:
  1. 编写启动方法,运行pytest

pytest.main([allure_demo.py, "--alluredir", "report/result"])

  1. 使用进程,开启allure服务

import subprocess subprocess.call('allure generate report/result/ -o report/html --clean', shell=True) subprocess.call('allure open -h 127.0.0.1 -p 9999 ./report/html', shell=True)


(两种方法都需要安装allure工具)

五、报告效果图及注解

============================= 提升自己 ==========================
进群交流、获取更多干货, 请关注微信公众号:

> > > 咨询交流、进群,请加微信,备注来意:sanshu1318 (←点击获取二维码)
> > > 学习路线+测试实用干货精选汇总:
www.cnblogs.com/upstudy/p/15859768.html
> > > python+requests+Pytest+Excel+Allure,测试都在学的热门技术:
www.cnblogs.com/upstudy/p/15921045.html
> > > 项目实战、简历、笔试题、面试题、职业规划:
www.cnblogs.com/upstudy/p/15901367.html
> > > 声明:如有侵权,请联系删除。
============================= 升职加薪 ==========================
更多干货,正在挤时间不断更新中,敬请关注+期待。

本文共计1368个文字,预计阅读时间需要6分钟。

自动化基础中,如何详细解析allure用例描述并实战应用?

前言 + allure 可以为pytest提供高质量的测试报告,并且可以与pytest完美结合。不仅支持页面渲染,还可以控制用例的执行。以下将对allure的使用进行详细介绍和总结。+ 需要准备的环境:

自动化基础中,如何详细解析allure用例描述并实战应用?

+ 系统环境:Python 3.6及以上版本+ 测试框架:pytest+ allure:安装allure-pytest插件

前言

allure可以输出非常精美的测试报告,也可以和pytest进行完美结合,不仅可以渲染页面,还可以控制用例的执行。下面就对allure的使用进行一个详细的介绍和总结。

需要准备的环境:

  • python
  • pytest
  • allure-pytest
  • allure工具
一、allure用例描述 使用方法 参数值 参数说明 @allure.epic() epic描述 敏捷里面的概念,对用例或用例集进行描述分类 @allure.feature() 模块名称 与epic类似,只是比epic级别低 @allure.story() 用户故事 与epic类似,只是比feature级别低 @allure.title(用例的标题) 用例的标题 重命名html报告的用例名称 @allure.testcase() 测试用例的链接地址 与link类似 @allure.issue() 缺陷 与link类似 @allure.description() 用例描述 进行测试用例的描述 @allure.step() 操作步骤 进行测试用例的步骤 @allure.severity() 用例等级 blocker,critical,normal,minor,trivial @allure.link() 链接 定义一个链接,在测试报告展现(推荐使用) @allure.attachment() 附件 报告添加附件 二、allure实战demo

# -*- coding:utf-8 -*- """ @File: allure_demo.py @Author: 三叔测试笔记 @Time : 2022/3/18 8:34 @Description: allure使用介绍 """ import pytest import allure from base.log import Logger logger = Logger(logger_name='allure', level='error').get_logger() @pytest.fixture(scope="session") # 用例前置操作 def login_fixture(): # 比如登录获取token操作 return "token:xx" @allure.step("用例步骤1") def step_1(): logger.info("用例操作---------------步骤1") return True @allure.step("用例步骤2") def step_2(): logger.info("用例操作---------------步骤2") return False @allure.step("用例步骤3") def step_3(): logger.info("用例操作---------------步骤3") return True @allure.epic("可以对用例或用例集进行描述分类(若出现多个时,内容一致则自动归为一类)") @allure.feature("对用例集或用例进行描述分类---与epic类似,只是比epic级别低") @allure.story("对用例集或用例进行描述分类---与epic类似,只是比feature级别低") class TestAllureDemo: @allure.testcase("xxx/testcase/list", name='用例链接testcase') # 为了更好的链接到问题分类或者bug、测试用例地址中(url、name两个参数,可不填写name;可以用@allure.link) @allure.link("xxx/testcase/list", name='用例链接link') # 与testcase没有多大区别,从可读性角度还是建议选择@allure.link @allure.issue("xxx/testcase/list", name='用例链接issue') # 与testcase区别在于有小虫子图标 @allure.title("用例的标题") # 可参数化标题 @allure.story("用例分类:1") # 可参数化标题 @allure.severity("critical") # 用例等级(blocker critical normal minor trivial) def test_case_1(self, login_fixture): """ 1.用例描述 2.用例步骤 3.预期结果 """ logger.info(login_fixture) # 获取用例前置的信息,比如登录token assert step_1() assert step_2() @allure.story("用例分类:2") def test_case_2(self, login_fixture): logger.info("测试用例2") assert step_1() assert step_3() @allure.epic("冒烟自动化用例") class TestDemo2: @allure.story("用例分类:3") def test_case_3(self, login_fixture): logger.info("测试用例3") step_1() @allure.story("用例分类:4") def test_case_4(self, login_fixture): logger.info("测试用例4") step_3() 三、allure的命令行参数

pytest执行用例时可以加上allure的标记参数,可以控制执行哪些用例。

--allure-severities=SEVERITIES_SET Comma-separated list of severity names. Tests only with these severities will be run. Possible values are: blocker, critical, normal, minor, trivial. --allure-epics=EPICS_SET Comma-separated list of epic names. Run tests that have at least one of the specified feature labels. --allure-features=FEATURES_SET Comma-separated list of feature names. Run tests that have at least one of the specified feature labels. --allure-stories=STORIES_SET Comma-separated list of story names. Run tests that have at least one of the specified story labels. --allure-link-pattern=LINK_TYPE:LINK_PATTERN Url pattern for link type. Allows short links in test, like 'issue-1'. Text will be formatted to full url with python str.format(). 实例如下:

# 选择运行你要执行epic的用例 pytest --alluredir ./report/allure --allure-epics=epic对大Story的一个描述性标签 # 选择运行你要执行features的用例 pytest --alluredir ./report/allure --allure-features=模块2 # 选择运行你要执行features的用例 pytest --alluredir ./report/allure --allure-stories="用户故事:1" 四、执行脚本,allure生成报表,并启动报告 运行方式一:

  1. 命令行模式下运行pytest,生产测试结果文件
    pytest demo.py --alluredir ./report/allure

  2. allure程序启动已经生产的文件
    allure serve ./report/allure

运行方式二:
  1. 编写启动方法,运行pytest

pytest.main([allure_demo.py, "--alluredir", "report/result"])

  1. 使用进程,开启allure服务

import subprocess subprocess.call('allure generate report/result/ -o report/html --clean', shell=True) subprocess.call('allure open -h 127.0.0.1 -p 9999 ./report/html', shell=True)


(两种方法都需要安装allure工具)

五、报告效果图及注解

============================= 提升自己 ==========================
进群交流、获取更多干货, 请关注微信公众号:

> > > 咨询交流、进群,请加微信,备注来意:sanshu1318 (←点击获取二维码)
> > > 学习路线+测试实用干货精选汇总:
www.cnblogs.com/upstudy/p/15859768.html
> > > python+requests+Pytest+Excel+Allure,测试都在学的热门技术:
www.cnblogs.com/upstudy/p/15921045.html
> > > 项目实战、简历、笔试题、面试题、职业规划:
www.cnblogs.com/upstudy/p/15901367.html
> > > 声明:如有侵权,请联系删除。
============================= 升职加薪 ==========================
更多干货,正在挤时间不断更新中,敬请关注+期待。