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. Windows中的用户和组以及用户密码处理

    目录 用户帐户 Windows 默认账户 Windows 内置用户账户 查看.创建和删除账户 组账户 内置组账户 组的查看.创建和删除 Windows中对用户密码的处理 LM-hash NTLM-ha ...

  2. 续订Jetbrain学生包

    今天打开IDEA和Pycharm都不约而同的告诉我我的账号无法使用学生包了 此刻我的内心是: 冷静下来我算了算,嗷,原来是一年的订阅期到了,那就简单了,直接续订吧,唉.其实续订和重新认证是一样的. 首 ...

  3. Thinking in UML 笔记(一) -- 面向对象

    一.UML 中最重要的就是面向对象. 面向对象的认识论可以构建更为复杂的系统来解释复杂的世界. 1. 面向过程,一切都是相互紧密地联系在一起,互相作用,互相影响. 2.面向对象, 世界是分割开的,只有 ...

  4. 认识WPF

    新开一节WPF桌面开发的讲解,这节先初步认识一下什么是WPF. 1.简介 WPF是 Windows Presentation Foundation 的英文缩写,意为"窗体呈现基础" ...

  5. Django(10)ORM模型介绍

    前言 随着项目越来越大,采用写原生SQL的方式在代码中会出现大量的SQL语句,那么问题就出现了: 1.SQL语句重复利用率不高,越复杂的SQL语句条件越多,代码越长.会出现很多相近的SQL语句. 2. ...

  6. Go的Waitgroup和锁

    学 Go 的时候知道 Go 语言支持并发,最简单的方法是通过 go 关键字开启 goroutine 即可.可在工作中,用的是 sync 包的 WaitGroup,然而这样还不够,当多个 gorouti ...

  7. 调用免费API查询全年工作日、周末、法定节假日、节假日调休补班数据

    前言 日常开发中,难免会用到判断今天是工作日.周末.法定节假日.节假日调休补班做一些业务处理,例如:仅在上班时间给用户推送消息.本文记录调用免费API查询全年工作日.周末.法定节假日.节假日调休补班数 ...

  8. Java集合,扑克牌的小项目练习

    Java集合,扑克牌的小项目练习 2小时学完了类与集合,一直二倍加跳过,集合和类的学习我觉得得多实践中去记住,光靠背,永远也背不完,学的时候记一下常用的,特殊的就行了,用的时候再查,多写代码才能会,哈 ...

  9. 真正的原生JS数据双向绑定(实时同步)

    真正的原生JS数据双向绑定(实时同步) 接触过vue之后我感觉数据双向绑定实在是太好用了,然后就想着到底是什么原理,今天在简书上看到了一位老师的文章 js实现数据双向绑定 然后写出了我自己的代码 wi ...

  10. Azure DevOps(一)利用Azure DevOps Pipeline 构建应用程序镜像到AWS ECR

    一,引言 最近项目上让开始学习AWS,作为一名合格的开发人员,当然也是学会利用Azure DevOps Pipeline 将应用程序部署到 AWS ECS(完全托管的容器编排服务).我们要学会将应用程 ...