浅谈Pytest中的marker

2023-02-15,,

浅谈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运行

通过命令行参数-m即可

比如现在有这个case

import pytest

@pytest.mark.login
def test_demo1():
assert True @pytest.mark.logout
def test_demo2():
assert True

contest.py

def pytest_configure(config):
config.addinivalue_line("markers", "login: login test demo",)
config.addinivalue_line("markers", "logout: logout test demo")# 注意一个marker要写一个addinivalue_line

运行的时候带上-m login即可选择登录用例进行测试

而-m的语法还比较复杂,可以参考-k的

general:
-k EXPRESSION
only run tests which match the given substring expression. An expression is a python evaluatable
expression where all names are substring-matched against test names and their parent classes.
Example: -k 'test_method or test_other' matches all test functions and classes whose name
contains 'test_method' or 'test_other', while -k 'not test_method' matches those that don't
contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the
matches. Additionally keywords are matched to classes and functions containing extra names in
their 'extra_keyword_matches' set, as well as functions which have names assigned directly to
them. The matching is case-insensitive.
-m MARKEXPR   only run tests matching given mark expression.For example: -m 'mark1 and not mark2'.

关于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中处理warning的方式考虑单独开个章节讲下 TODO

浅谈Pytest中的marker的相关教程结束。

《浅谈Pytest中的marker.doc》

下载本文的Word格式文档,以方便收藏与打印。