1、标记mark说明
- pytest支持自定义标记,自定义标记可以把一个web项目划分成多个模块,然后指定模块名执行
- 再比如:我们可以指定哪些用例是windows下执行的,哪些用例是mac下执行的,然后在代码运行的时候指定mark即可
2、代码演示
import pytest
@pytest.mark.weibo
def test_weibo():
print("===正在测试微博===")
@pytest.mark.toutiao
def test_toutiao():
print("===正在测试头条===")
@pytest.mark.toutiao
def test_toutiao2():
print("===再次测试头条===")
@pytest.mark.xinlang
def test_skip():
print("===正在测试新浪===")
def test_no_mark():
print("===没有打标记===")
3、指定模块运行
- 执行标记为weibo的用例
pytest -s -m xinlang test_01.py
- 执行结果
=========================================================================== test session starts ============================================================================
platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark, configfile: pytest.ini
collected 5 items / 4 deselected / 1 selected
test_01.py ===正在测试新浪===
.
============================================================================= warnings summary =============================================================================
test_01.py:25
/Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark/test_01.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.xinlang - 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.xinlang
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================ 1 passed, 4 deselected, 1 warning in 0.01s ================================================================
避免结果中的warming
不想执行标记为xinlang的用例
pytest -s -m "not xinlang" test_01.py
=========================================================================== test session starts ============================================================================
platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark, configfile: pytest.ini
collected 5 items / 1 deselected / 4 selected
test_01.py ===正在测试微博===
.===正在测试头条===
.===再次测试头条===
.===没有打标记===
.
============================================================================= warnings summary =============================================================================
test_01.py:25
/Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark/test_01.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.xinlang - 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.xinlang
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================ 4 passed, 1 deselected, 1 warning in 0.01s ================================================================
执行多个自定义标记的用例
pytest -s -m "weibo or toutiao" test_01.py
=========================================================================== test session starts ============================================================================
platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark, configfile: pytest.ini
collected 5 items / 2 deselected / 3 selected
test_01.py ===正在测试微博===
.===正在测试头条===
.===再次测试头条===
.
============================================================================= warnings summary =============================================================================
test_01.py:25
/Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark/test_01.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.xinlang - 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.xinlang
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================ 3 passed, 2 deselected, 1 warning in 0.01s ================================================================
参考文章:https://www.cnblogs.com/poloyy/p/12669068.html
|