pytest简易教程(12):mark标记测试用例
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
前言
通常,我们通过分包或者分模块来对用例进行分类管理,
如果只想执行符合某要求的部分用例,该如何实现呢?
可以使用装饰器@pytest.mark.xxx给用例打标签(自定义标记)。
自定义标记使用流程
1. 注册自定义标记(通过pytest.ini进行管理)
2. 将模块、函数、类、方法进行业务标记
3. 根据自定义标记运行用例
注册自定义标记
在pytest.ini文件中,新建标记:
1 2 3 4 5 6 7 | [pytest] markers = user: user marker product: product marker findproduct deleteproduct modulex: modulex marker |
说明:
1 2 3 | 1.标记名要是英文命名,建议参考模块命名,要有业务含义 2.所有自定义标记,建议在pytest.ini中进行统一管理,并通过命令参数--strict-markers进行授权(非必须) 3.pytest中的markers配置相当于我们对用例的一种归类 |
开启严格标记,如果标记不在配置文件中,会报错;
不开启严格标记,如果标记不在配置文件中,会warning;
1 2 3 4 5 6 7 8 | [pytest] addopts = --strict-markers markers = user: user marker product: product marker findproduct deleteproduct modulex: modulex marker |
获取现有标记: pytest --markers,包含自定义和内置,前面几个是我们刚刚创建的
自定义标记分类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : 韧 # @wx :ren168632201 # @Blog :https://www.cnblogs.com/uncleyong/ import pytest pytestmark = pytest.mark.modulex # 模块类标记 # pytestmark = [pytest.mark.modulex, pytest.mark.moduley] # 可以注册多个 @pytest.fixture( params =[ '韧' , '全栈测试笔记' , '性能测试' ]) def fun(request): # 必须是request这个参数名 return request.param # 依次取列表中的每个值返回 @pytest.fixture( params =[ 'apple' , 'car' ]) def fun2(request): return request.param @pytest.mark.user # 函数标记 def test_user_reg(fun): print(f "---test_user_reg: {fun}" ) @pytest.mark.user def test_user_login(): print( "---test_user_login" ) @pytest.mark.product # 类标记 class TestProduct: @pytest.mark.findproduct # 方法标记 def test_product_find(self, fun2): print(f "---test_product_find: {fun2}" ) @pytest.mark.deleteproduct def test_product_delete(self): print( "---test_product_delete" ) |
说明:
模块、函数、方法、类上都可以标记多个。
模块标记方式是:
1 | pytestmark = [pytest.mark.modulex, pytest.mark.moduley] |
函数、方法、类标记方式是:
1 2 | @pytest.mark.module1 @pytest.mark.module2 |
根据标记运行用例
1 2 3 4 5 6 7 8 | [pytest] addopts = -s --strict-markers markers = user: user marker product: product marker findproduct deleteproduct modulex: modulex marker |
运行所有:
指定marker运行:
pytest case\test_qzcsbj.py -m user
或者:pytest case\test_qzcsbj.py -m=user
指定多个marker运行:
pytest case\test_qzcsbj.py -m "user or deleteproduct"
或者:pytest case\test_qzcsbj.py -m="user or deleteproduct"
排除某个marker运行:
pytest case\test_qzcsbj.py -m "not user"
或者:pytest case\test_qzcsbj.py -m="not user"
__EOF__

关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容