1.给用例添加自定义标签命令:@pytest.mark.tagname     #tagname是自定义的标签名

import pytest

class TestClass():
@pytest.mark.smoke
def test_one(self):
print("test_one方法执行")
assert 1==1
@pytest.mark.回归测试
def test_two(self):
print("test_two方法执行")
assert 'o' in 'love'

2.根据标签运行测试用例:pytest -m tagname

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -m smoke -s
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items / 1 deselected / 1 selected test_m1.py test_one方法执行
. ============================================================================================================= warnings summary =============================================================================================================
c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325
c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning, c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325
c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325: PytestUnknownMarkWarning: Unknown pytest.mark.回归测试 - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning, -- Docs: https://docs.pytest.org/en/latest/warnings.html
=============================================================================================== 1 passed, 1 deselected, 2 warnings in 0.22s ================================================================================================

3.因为自定义的标签没有注册,所以在运行时会出现警告的信息,pytest注册标签有两种方法

(1):注册pytest.ini文件(在当前目录创建pytest.ini文件)

[pytest]
markers=
smoke:冒烟测试 #冒号后面是标签的描述,可不填
two
回归测试 #如果标签是中文,编码需对应进行修改

再看下运行结果:警示信息没有了

C:\Users\cale\checkapi\test_cc>pytest test_m1.py  -s
========================================================================= test session starts =========================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc, inifile: pytest.ini
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items test_m1.py test_one方法执行
.test_two方法执行
. ========================================================================== 2 passed in 0.13s ==========================================================================

(2):写钩子到conftest.py(在命令行当前目录下创建conftest.py)

def pytest_configure(config):
config.addinivalue_line("markers",'smoke')
config.addinivalue_line("markers", '回归测试')

运行结果如下

C:\Users\cale\checkapi\test_cc>pytest test_m1.py  -s
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items test_m1.py test_one方法执行
.test_two方法执行
. ============================================================================================================ 2 passed in 0.09s =============================================================================================================

 4.在一个测试用例上打多个标签

import pytest

@pytest.mark.smoke
@pytest.mark.回归测试
def test_one(self):
print("test_one方法执行")
assert 1==1

5.给测试类打标签

import pytest

#方式一
@pytest.mark.smoke
class TestClass():
def test_one(self):
print("test_one方法执行")
assert 1==1
#方式二
class TestClass():
    pytestmark=[pytest.mark.smoke,pytest.mark.回归测试] #多个标签存在列表里面
      def test_one(self):
print("test_one方法执行")
assert 1==1

pytest 给用例打标签的更多相关文章

  1. Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.

    近日,使用Pytest+Appium 实现APP端UI自动化,遇到Pytest收集用例失败的情况. 报错信息如下: test_room.py:None (test_room.py) ImportErr ...

  2. pytest执行用例时从conftest.py抛出ModuleNotFoundError:No module named 'XXX'异常的解决办法

    一.问题描述 在项目根目录下执行整个测试用例,直接从conftest.py模块中抛出了ModuleNotFoundError:No module named 'TestDatas'的异常: 二.解决方 ...

  3. pytest执行用例:明明只写了5个测试用例, 怎么收集到33个!?

    pytest收集测试用例的顺序: 同一个项目中搜索所有以test_开头的测试文件.test_开头的测试类.test_开头的测试函数 执行测试用例的顺序: 是按照先数据(0~9)>再字母(a~z) ...

  4. pytest 失败用例重试

    https://www.cnblogs.com/jinzhuduoduo/articles/7017405.html http://www.lxway.com/445949491.htm https: ...

  5. pytest系列(二):筛选用例新姿势,mark 一下,你就知道。

    pytest系列(一)中给大家介绍了pytest的特性,以及它的编写用例的简单至极. 那么在实际工作当中呢,我们要写的自动化用例会比较多,不会都放在一个py文件里. 如下图所示,我们编写的用例存放在不 ...

  6. pytest文档32-allure描述用例详细讲解

    前言 pytest+allure是最完美的结合了,关于allure的使用,本篇做一个总结. allure报告可以很多详细的信息描述测试用例,包括epic.feature.story.title.iss ...

  7. pytest--mark基本使用(主要通过pytest.ini文件注册标签名,对用例进行标记分组)

    1.pytest中的mark介绍 mark主要用于在测试用例/测试类中给用例打标记(只能使用已注册的标记 名),实现测试分组功能,并能和其它插件配合设置测试方法执行顺序等.如下 图,现在需要只执行红色 ...

  8. pytest之收集用例规则与运行指定用例

    前言 上篇文章相信大家已经了解了pytest在cmd下结合各种命令行参数如何运行测试用例,并输出我们想要看到的信息.那么今天会讲解一下pytest是如何收集我们写好的用例?我们又有哪些方式来运行单个用 ...

  9. Pytest权威教程26-示例和自定义技巧

    目录 示例和自定义技巧 返回: Pytest权威教程 示例和自定义技巧 这是一个(不断增长的)示例列表.如果你需要更多示例或有疑问,请联系我们.另请参阅包含许多示例代码段的 综合文档.此外,stack ...

随机推荐

  1. Word 通过添加Package 实现word藏毒

    这个思路要结合近期在一些安全网站上公布的姿势来实现,先科普几个地方. (1)通过cmd本身就可以直接下载: Bitsadmin /transfer AA /download /priority nor ...

  2. 记一次CTF的签到题

    开篇 打开题目网站 首先看到的是一个人博客,功能点非常少,功能较多的页面就是留言板了 一开始没啥思路,就想着抓包能不能找到SQL注入无果,在这个地方卡了很久 柳暗花明 在乱点的时候,无意中发现题目中的 ...

  3. Mybatis的初始化和结合Spring Framework后初始化的源码探究

    带着下面的问题进行学习: (1)Mybatis 框架或 Spring Framework 框架对数据层 Mapper 接口做了代理,那是做了 JDK 动态代理还是 CGLIB 代理? (2)Mappe ...

  4. 安装mysql警告: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

    CentOS安装rpm安装MySQL时爆出警告: warning: mysql-community-server-5.7.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 ...

  5. 大学四年因为分享了这些软件测试常用软件,我成了别人眼中的(lei)大神(feng)!

    依稀记得,毕业那天,我们辅导员发给我毕业证的时候对我说"你可是咱们系的风云人物啊",哎呀,别提当时多开心啦????,嗯,我们辅导员是所有辅导员中最漂亮的一个,真的???? 不过,辅 ...

  6. mac Idea快捷键小记

    重写父类方法:control + o 实现父类方法:control + i 最全的一个按键:control + 回车

  7. 解读vue-server-renderer源码并在react中的实现

    前言 ​ 在博客开发的过程中,有这样一个需求想解决,就是在SSR开发环境中,服务端的代码是是直接通过webpack打包成文件(因为里面包含同构的代码,就是服务端与客户端共享前端的组件代码),写到磁盘里 ...

  8. 不同规模的企业对CRM的需求是否相同?

    CRM客户管理系统在我们的认知中往往是中大型企业的选择.如今,越来越多中小规模企业开始使用CRM系统.CRM的功能随着发展变得越来越实用,可以满足不同行业不同业务规模的企业的需求.同时,CRM功能类型 ...

  9. CRM帮助初创企业降本增效的四个方法

    对大部分初创公司来说,只有少数企业能够实现盈利,大部分只能维持盈亏平衡甚至是亏损.这是因为初创企业很难在短时间之内找到稳定的赢利点,而企业面临的风险和投入又是无法预知的.初创企业想要快速盈利,只能降低 ...

  10. envoy 官方example运行失败问题处理

    镜像内安装包失败处理 方法一:修改Dockerfile,在Dockerfile中增加如下 ubuntu示例 RUN sed -i 's/archive.ubuntu.com/mirrors.aliyu ...