"""1.指定当前目录 具体模块 pytest .\test_01_first.py""" """1.指定执行某个目录 pytest 目录/""" # pytest -k "test_1 or TestCase" // 参数k :主要搜索 当前目录的 函数名 或 类名 ,可以使用 and or 做逻辑筛选 # pytest -v 文件名 -k "test_1 or TestCase" // 参数k :主要搜索 当前目录的 函数名 或 类名 ,可以使用 and or 做逻辑筛选 # pytest - v.\01_01_pytest_book\ -k "test_1"
------------------
test_06_-m参数执行指定标记的用例.py , 文件,代码如下:
import pytest """-m 全称 market,可以通过方法名称的表达式灵活筛选测试某种方法,和不测试相关方法""" """ pytest -v XXX.py pytest -v -m 'p0' XXX.py pytest -v -m 'not p0' XXX.py pytest -v -m 'not p0 and not func' XXX.py pytest -v -m 'func or postgres' XXX.py """ @pytest.mark.ps def test_sth_01(): print("test_sth_01") @pytest.mark.func def test_sth_02(): print("test_sth_02") @pytest.mark.postgres def test_sth_03(): print("test_sth_03") """1. 执行 pytest -v .\test_06_-m参数执行指定标记的用例.py (警告信息可以忽略)""" # PS E:\learnPython\01_01_pytest_book> pytest -v .\test_06_-m参数执行指定标记的用例.py # ================================================= test session starts ===================================== # platform win32 -- Python 3.11.3, pytest-7.4.2, pluggy-1.3.0 -- E:\python\python.exe # cachedir: .pytest_cache # rootdir: E:\learnPython\01_01_pytest_book # collected 3 items # # test_06_-m参数执行指定标记的用例.py::test_sth_01 PASSED [ 33%] # test_06_-m参数执行指定标记的用例.py::test_sth_02 PASSED [ 66%] # test_06_-m参数执行指定标记的用例.py::test_sth_03 PASSED [100%] """2.执行 包含ps标记的用例 pytest -v -m 'ps' .\test_06_-m参数执行指定标记的用例.py """ # PS E:\learnPython\01_01_pytest_book> pytest -v -m 'p1' .\test_06_-m参数执行指定标记的用例.py # ================================================= test session starts ============================================== # platform win32 -- Python 3.11.3, pytest-7.4.2, pluggy-1.3.0 -- E:\python\python.exe # cachedir: .pytest_cache # rootdir: E:\learnPython\01_01_pytest_book # collected 3 items / 2 deselected / 1 selected # # test_06_-m参数执行指定标记的用例.py::test_sth_p1 PASSED [100%] # ================================================== warnings summary ================================================ # 新增配置文件:pytest.ini文件注册标记,可以去除标记警告,具体配置语法如下: # [pytest] # # markers = # main # 标记2 # # 用例前面加载标签:@pytest.mark.标签名 # @pytest.mark.main # def test_demo(): # pass """3.执行执行不包含 ps标记的用例 pytest -v -m 'not ps' .\test_06_-m参数执行指定标记的用例.py """ # PS E:\learnPython\01_01_pytest_book> pytest -v -m "not ps" .\test_06_-m参数执行指定标记的用例.py # ================================================= test session starts ============================================= # platform win32 -- Python 3.11.3, pytest-7.4.2, pluggy-1.3.0 -- E:\python\python.exe # cachedir: .pytest_cache # rootdir: E:\learnPython\01_01_pytest_book # collected 3 items / 1 deselected / 2 selected # # test_06_-m参数执行指定标记的用例.py::test_sth_02 PASSED [ 50%] # test_06_-m参数执行指定标记的用例.py::test_sth_03 PASSED [100%] # # ================================================== warnings summary =============================================== # test_06_-m参数执行指定标记的用例.py:12 # E:\learnPython\01_01_pytest_book\test_06_-m参数执行指定标记的用例.py:12: PytestUnknownMarkWarning: # Unknown pytest.mark.ps - 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.ps # # test_06_-m参数执行指定标记的用例.py:18 # E:\learnPython\01_01_pytest_book\test_06_-m参数执行指定标记的用例.py:18: PytestUnknownMarkWarning: Unknown pytest. # mark.func - 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.func # # test_06_-m参数执行指定标记的用例.py:23 # E:\learnPython\01_01_pytest_book\test_06_-m参数执行指定标记的用例.py:23: PytestUnknownMarkWarning: # Unknown pytest.mark.postgres - 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.postgres # # -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html # ===================================== 2 passed, 1 deselected, 3 warnings in 0.05s ================================