pytest命令执行

本文介绍了pytest工具的一些常用命令行参数,如指定执行特定测试用例、控制输出信息的详细程度、在失败后停止运行以及使用标记来筛选执行的用例。同时,强调了自定义标记需要在配置文件中注册才能生效,否则会导致运行警告或错误。
摘要由CSDN通过智能技术生成

以下执行命令参数是我比较常用的,其他更多参数用法可通过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 --fixtures

  • **pytest --fixtures **: 展示系统和自己定义的fixture

pytest -q/–quiet

  • **pytest -q/–quiet **: 输出简洁结果

pytest -x

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

pytest -v

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

pytest -vv

  • pytest -vv: 在 -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 ==============================================================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值