pytest命令执行

以下执行命令参数是我比较常用的,其他更多参数用法可通过pytest --help查看

选择指定文件或者case执行

  • pytest test_mod.py::test_func或者pytest test_mod.py::TestClass::test_method :指定执行指定用例
  • pytest test_mod.py或者pytest test_mod.py::TestClass :指定执行test_mod.py 或者 test_mod.py中的TestClass类

pytest -x

pytest -x: stop after first failure,运行用例过程中失败就停止

pytest -v

pytest -v: 展示运行过程中的详情

pytest --lf

pytest --lf: 只运行上一次失败的case,如果没有失败的则运行所有的case

pytest --ff

pytest --ff: 会运行所有的case, 但是优先运行上一次运行失败的用例

pytest --collect-only

pytest --collect-only: 只收集用例,不会执行

pytest --maxfail

pytest --maxfail=2 # stop after two failures

pytest -k

pytest -k “string” :匹配文件名、类名、函数名包含string的,不区分大小写
比如:

  • -k '‘test_method or test_other’: 匹配函数名/类名包含test_method或者test_other
  • -k ‘not test_method’: 匹配函数名/类名不包含test_method
  • -k ‘not test_method and not test_other’ : 匹配函数名/类名不包含test_method 和 test_other的

pytest --markers

pytest --markers: 展示所有的markers

pytest -m

pytest -m first : 执行标记为first的用例,用**@pytest.mark.first进行装饰,对于自定义的标记,需要在pytest.ini文件或者conftest.py文件**中添加对应的标记
pytest.ini文件:

[pytest]
markers=first:mark run first
        second:mark run second

conftest.py中:

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "dev: run in dev env"
    )
    config.addinivalue_line(
        "markers", "test: run in test env"
    )

⚠️如果自定义的标记未进行注册,运行时有警告,不会报错。如果运行参数加上--strict-markers 或者在pytest.ini文件中添加如下代码,运行时就会报错,而不执行用例

[pytest]
addopts = --strict-markers

错误:

=================================================================================== ERRORS ===================================================================================
______________________________________________________________________ ERROR collecting test_pytest.py _______________________________________________________________________
'test1' not found in `markers` configuration option
========================================================================== short test summary info ===========================================================================
ERROR test_pytest.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================== 1 error in 0.17s ==============================================================================

你可能感兴趣的:(经验分享,python,pytest,python)