Pytest学习8 -使用自定义标记mark
2020-11-16 本文已影响0人
C1R2
前言
pytest 可以支持自定义标记,自定义标记可以把一个 web 项目划分多个模块,然后指定模块名称执行
譬如我可以标明哪些用例是window下执行的,哪些用例是mac下执行的,在运行代码时候指定mark即可
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
@pytest.mark.weibo
def test_weibo():
print("测试微博")
@pytest.mark.toutiao
def test_toutiao():
print("测试头条")
@pytest.mark.toutiao
def test_toutiao1():
print("再次测试头条")
@pytest.mark.xinlang
class TestClass:
def test_method(self):
print("测试新浪")
def testnoMark():
print("没有标记测试")
if __name__=='__main__':
pytest.main(['-s','08_mark.py','-m=weibo'])
pytest.main(['-s','文件名','-m=标记名'])
cmd敲运行命令:
pytest -s -m weibo 08_mark.py
如何避免warnings
创建一个pytest.ini文件(后续详解)
加上自定义mark,如下图
注意:pytest.ini需要和运行的测试用例同一个目录,或在根目录下作用于全局
[pytest]
markers =
weibo: this is weibo page
不想执行标记weibo的用例,直接取反即可
pytest -s -m "not weibo" 08_mark.py
执行多个自定义标记的用例
pytest -s -m "toutiao or weibo" 08_mark.py