pytest mark的使用

一、使用方法

  1. 注册标签名
  2. 在测试用例/测试类前面加上:@pytest.mark.标签名
  3. 运行时候指定打标记范围:测试用例、测试类、模块文件

二、注册标签

方式1、单个标签:

conftest.py添加如下代码:

def pytest_configure(config):

   # demo是标签名

  config.addinivalue_line("markers", "smoke:冒烟用例")

方式2、多个标签:

conftest.py添加如下代码:

def pytest_configure(config):

   marker_list = ["testorder", "testproduct", "smoke"] # 标签名集合

  for markers in marker_list: config.

     addinivalue_line("markers", markers)

方式3、添加pytest.ini 配置文件(在你项目的任意一个文件下,新建一个file,文件命名为pytest.ini)

[pytest]

markers=

          smoke:冒烟用例

          order:订单用例

          product:商品用例

三、使用标签

可以在类中

可以在方法中

 可以在函数中

四、运行方式

1、命令行模式

通过标记表达式执行 pytest -m login 这条命令会执行被装饰器@pytest.mark.login装饰的所有测试用例(类级别\函数级别\用例级别)

 2、新建run.py文件运行,代码如下:

if __name__ == '__main__':
pytest.main(["-m","login","-vs"])  

3、新建pytest.ini 文件

原文地址:https://www.cnblogs.com/keenajiao/p/15801468.html