测试框架pytest(二)

接上一篇,继续延伸pytest框架

打标签

在测试中,我们对用例做一些标识,用来做冒烟测试,优先级最高,次级,再次级的标记,pytest支持打标签的方式来对用例进行分级管理,确保运行的是对应等级的用例。使用方式是在对应的用例上添加装饰器 @pytest.mark.xxx ,同一个用例支持添加多个标签并且支持给整个类打上一个标签

test_demo.py

import pytest


class TestDemo:
  @pytest.mark.smoke
  @pytest.mark.level1
def test_demo(self): a = 5 b = -1 assert a != b print("我的第一个测试用例")   
  
  @pytest.mark.level1
def test_two(self): a = 2 b = -1 assert a != b print("我的第二个测试用例") if __name__ == "__main__": pytest.main(['test_demo.py', '-m', 'smoke'])

python运行方式:

python test_demo.py

pytest运行方式

pytest -v -m smoke test_demo.py

上述运行方式在pycharm会有warning信息,要解决这个报错的话,可以通过注册标签的方式来实现,具体如下

在目录下新增pytest.ini文件,然后文件中写入如下内容

[pytest]
markers = 
smoke: smoke tag
level1: level1 cases

然后再次运行

 

posted @ 2022-04-25 11:55  逗蚂蚁  阅读(86)  评论(0)    收藏  举报