# 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
[pytest]
markers:
login: login test demo
创建一个conftest.py
def pytest_configure(config):
config.addinivalue_line(
"markers", "login: login test demo"
)
效果是一样的
通过命令行参数-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'.
查询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
在性能测试过程中总会收到一些需求如:单接口每秒并发20,这种并发持续60秒,通过负载测试查看系统稳定性,今天就让我们来浅谈一下这种场景如何去实现性能测试~