pytest用法之使用mark对测试方法分类

经过使用pytest.markhelper您能够轻松地在测试函数上设置元数据。也就是说,经过@pytest.mark可以控制须要执行知足哪些特征的测试,例如在执行test前增长修饰@pytest.mark.website。html

标记mark只能用于测试,对fixture没有影响。有关fixture的做用,下篇中再介绍。web

一些内置的mark

pytest用法之使用mark对测试方法分类

注册mark

您能够在pytest.ini文件像这样的文件:app

[pytest]
markers=slow:标识测试速度极慢的那些测试

或者在你的pyproject.toml项目像这样表达:
pytest用法之使用mark对测试方法分类ide

在未知标记上引起错误

未注册的标记(例如 @pytest.mark.name_of_the_mark的修饰器)将始终发出警告,以免因为键入错误的名称而默默地作一些使人惊讶的事情。
When the --strict-markers command-line flag is passed, any unknown marks applied with the @pytest.mark.name_of_the_mark decorator will trigger an error. You can enforce this validation in your project by adding --strict-markers to addopts:
pytest用法之使用mark对测试方法分类函数

举例

pytest用法之使用mark对测试方法分类

而后,能够将测试运行限制为仅运行标有webtest的测试:学习

pytest -v -m webtest

测试结果以下:测试

pytest用法之使用mark对测试方法分类

或者反过来,还能够运行除webtest以外的全部测试:this

pytest -v -m "not webtest"

小结1

注意到上面的“selected”了吗?在Pytest中,借助mark能够筛选要测试的部分测试,从而加速测试工做。3d

标记整个类或模块¶

你能够用pytest.mark带类的装饰器将标记应用于其全部测试方法:
pytest用法之使用mark对测试方法分类code

有兴趣的朋友能够参考引文中内容做更系列全面学习。

引用

https://docs.pytest.org/en/stable/example/markers.html#mark-examples

相关文章
相关标签/搜索