一、常见的内置markers
- usefixtures - 为测试函数或者测试类知名使用那些fixture
- filterwarnings - 为一个测试函数过滤一个指定的告警
- skip - 跳过一个测试函数
- skipif - 如果满足条件就跳过测试函数
- xfail - 标记用例失败
- parametrize - 参数化
二、查看所有markers
如下,可以查看到当前环境中的所有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 36 37 |
|
三、注册自定义marks
方式一:在pytest.ini中按照如下格式声明即可,冒号之前为注册的mark名称,冒号之后为此mark的说明
1 2 3 4 |
|
此时test_demo.py代码如下
1 2 3 4 5 6 7 8 9 10 11 12 |
|
使用pytest -m smoke执行结果如下,发现此时即只执行了标记为smoke的一个用例,这就是和自定义mark的使用方法
1 2 3 4 5 6 7 8 9 10 |
|
方式二:在conftest.py文件中重写pytest_configure函数即可,比如如下,注册两个mark:smoke和test
1 2 3 4 5 6 7 |
|
test_demo.py代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
通过pytest -m test 执行结果如下:
1 2 3 4 5 6 7 8 9 10 |
|
四、对未注册mark的限制
默认情况下,对未注册mark直接使用是会产生一条告警信息,比如这里把pytest.ini和conftest.py都删除掉,只剩test_demo.py一个文件
test_demo.py代码如下
1 2 3 4 5 6 7 8 9 10 11 12 |
|
直接使用 pytest -m smoke 执行结果如下,可以发现这里产生了两条告警,这就是因为这两条告警未在pytest.ini或者conftest.py中进行注册的原因,在实际项目开发中如果在执行测试的时候发现了这种大片告警打印,解决办法就是在pytest.ini或者conftest.py将这些告警报出的mark都进行注册即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
如果希望强制限制必须先注册再使用mark,则可以在pytest.ini中加上如下配置即可
1 2 |
|
比如test_demo.py代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
此时继续使用pytest -m smoke执行结果如下,发现此时已经报错了,即强制限制必须对mark进行注册
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】