浅谈Pytest中的marker
没有注册marker
- 我们写一个简单的测试
# test_demo.py
import pytest
@pytest.mark.login
def test_demo():
assert True
- 你运行的话会有如下提示
test_demo.py:4: PytestUnknownMarkWarning: Unknown pytest.mark.login - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
@pytest.mark.login
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
- 上面两个文档值得你的仔细阅读
注册marker方式一:pytest.ini
- 新建一个文件pytest.ini(在哪里创建?你应该要知道,跟你的项目结构有关),写入如下内容
[pytest]
markers:
login: login test demo
- 再次运行就不会有PytestUnknownMarkWarning
注册marker方式二:pytest_configure
-
创建一个conftest.py
def pytest_configure(config): config.addinivalue_line( "markers", "login: login test demo" )
-
效果是一样的
关于marker的其他
-
查询markers
pytest --markers # 能查到当前注册的markers
-
命令行参数--strict-markers
pytest.main(['-sv','--strict-markers',__file__]) # 强制markers必须要注册
-
或者放pytest.ini中
[pytest] addopts = --strict-markers
-
会产生如下信息
=================================== ERRORS ==================================== ________________________ ERROR collecting test_demo.py ________________________ 'login' not found in `markers` configuration option =========================== short test summary info =========================== ERROR test_demo.py !!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!! ============================== 1 error in 0.08s ===============================
-
marker是pytest中pick用例的多种方式之一(-m),其他pick用例的方式比如-k,--allure的几个
--allure-severities=SEVERITIES_SET --allure-epics=EPICS_SET --allure-features=FEATURES_SET --allure-stories=STORIES_SET --allure-ids=IDS_SET Comma-separated list of IDs. --allure-link-pattern=LINK_TYPE:LINK_PATTERN
标签:浅谈,--,pytest,markers,test,Pytest,marker,login From: https://www.cnblogs.com/wuxianfeng023/p/17095146.htmlpytest中处理warning的方式考虑单独开个章节讲下 TODO