分组执行
pytest的分组执行依旧可以用mark进行标记,标记名可以自定义的取,比如冒烟用例可以用smoke
。
看起来像是这样:
class TestLogin:
@pytest.mark.smoke
def test_01(self):
print('hello salmon')
# 同时使用两个装饰器,也是可以的。
@pytest.mark.run(order=2)
@pytest.mark.smoke
def test_03():
print('bing')
然后,我们在配置文件里添加上markers,这是分组,整体的pytest.ini
可能会是这样的
[pytest]
addopts = -vs
testpaths = ./test_pytest
python_files = test*.py
python_classes = Test*
python_functions = test
Markers =
smoke:冒烟用例
www:名字可以随意取
执行:我们只需要在执行时加一个-m
参数即可,-m
后面跟的是你要执行哪些组,比如你要执行smoke
pytest -m "smoke"
或者你想同时执行smoke和www
pytest -m "smoke or www"
亦或者再执行同时标记有smoke和www的
class TestLogin:
@pytest.mark.smoke
@pytest.mark.www
def test_01(self):
print('hello salmon')
pytest -m "smoke and www"
跳过用例
无条件跳过
同样的,我们使用mark进行标记
@pytest.mark.skip()
# 还可以说明一下,为什么跳过
@pytest.mark.skip(reason='asdasdasd')
有条件跳过
依旧是使用mark进行标记,只要满足了某个判断,就可以跳过
a = 1
@pytest.mark.skipif(a > 10, reason='因为大于10,所以跳过')
生成html报告
前提是,安装了pytest-html
在pytest.ini文件下,addopts之后添加--html 路径
[pytest]
addopts = -vs --html ./report/report.html
testpaths = ./test_pytest
python_files = test*.py
python_classes = Test*
python_functions = test
Markers =
smoke:冒烟用例
- Post link: https://www.godhearing.cn/pytest-jin-jie/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.