在Shell执行pytest -h可以看到pytest的命令行参数有这10大类,共132个

序号 类别 中文名 包含命令行参数数量
1 positional arguments 形参 1
2 general 通用 31
3 reporting 报告 18
4 collection 收集 15
5 test session debugging and configuration 测试session调试和配置 11
6 pytest-warnings pytest警告 1
7 logging 日志 11
8 reporting-allure allure测试报告 3
9 ini-options pytest.ini/tox.ini/setup.cfg
配置文件
37
10 environment variables 环境变量 4

1.positional arguments

file_or_dir

指定一个或多个文件/目录

pytest [file_or_dir] [file_or_dir] [...]

2.general

-k EXPRESSION

名字包含test_method或test_other的函数或类

-k "test_method or test_other"

名字不包含test_method

-k "not test_method"

名字不包含test_method不包含test_other

-k 'not test_method and not test_other'

大小写敏感。

源码这里不是很明白,先放这,以后再补充解释吧

@classmethod
def from_item(cls, item: "Item") -> "KeywordMatcher":
mapped_names = set() # Add the names of the current item and any parent items.
import pytest for node in item.listchain():
if not isinstance(node, (pytest.Instance, pytest.Session)):
mapped_names.add(node.name) # Add the names added as extra keywords to current or parent items.
mapped_names.update(item.listextrakeywords()) # Add the names attached to the current function through direct assignment.
function_obj = getattr(item, "function", None)
if function_obj:
mapped_names.update(function_obj.__dict__) # Add the markers to the keywords as we no longer handle them correctly.
mapped_names.update(mark.name for mark in item.iter_markers()) return cls(mapped_names)

-m MARKEXPR

包含mark1,不包含mark2

-m 'mark1 and not mark2'

--markers

显示markers

pytest --markers

-x, --exitfirst

第一个error或failed的test就退出,2个参数等价

pytest -x
pytest --exitfirst

--maxfail=num

2个errors或failures就退出

pytest --maxfail=2

--strict-config

解析配置文件中pytest部分时,遇到warning就抛出error

pytest --strict-config

-c file

my.ini文件加载配置

pytest -c my.ini

--continue-on-collection-errors

收集test失败,仍然强制继续执行

pytest --continue-on-collection-errors

--rootdir=ROOTDIR

tests根目录,相对路径

pytest --rootdir="root_dir"
pytest --rootdir="./root_dir"
pytest --rootdir="root_dir/another_dir/"

绝对路径

pytest --rootdir="/home/user/root_dir"

带变量

pytest --rootdir="$HOME/root_dir"

--fixtures, --funcargs

显示fixtures,以下等价

pytest --fixtures
pytest --funcargs

显示以_开头的fixture

pytest --fixtures -v

--fixtures-per-test

显示每个test用到的fixture

pytest --fixtures-per-test

--pdb

在errors或KeyboardInterrupt时,启用默认Python debugger

pytest --pdb

--pdbcls=modulename:classname

启用自定义Python debugger,由IPython.terminal.debugger module下的TerminalPdb class定义

pytest --pdbcls=IPython.terminal.debugger:TerminalPdb

--trace

run每个test时break,进入debugger交互

pytest --trace

--capture=method

文件描述符(默认)

pytest --capture=fd

stdout/stderr 内存

pytest --capture=sys

显示print

pytest --capture=no

tee-sys

pytest --capture=tee-sys

-s

等价于--capture=no

--runxfail

强制运行xfail标记的test

pytest --runxfail

--lf, --last-failed

重跑上次失败的tests,如果没有失败就重跑全部,以下等价

pytest -lf
pytest --last-failed

--ff, --failed-first

优先跑上次失败的test,tests的顺序会被打乱,以下等价

pytest -ff
pytest --failed-first

--nf, --new-first

优先跑新添加的tests,剩余的按文件mtime顺序,以下等价

pytest --nf
pytest --new-first

--cache-show=[CACHESHOW]

显示缓存,默认*

pytest --cache-show

显示缓存,带参数cache\nodeids

pytest --cache-show=cache\nodeids

--cache-clear

运行开始时清缓存

pytest --cache-clear

--lfnf={all,none}, --last-failed-no-failures={all,none}

没有last-failed缓存数据,或上次没有失败时,

跑全部tests

pytest --lfnf=all
pytest --last-failed-no-failures=all

不运行

pytest --lfnf=none
pytest --last-failed-no-failures=none

--sw, --stepwise

逐步运行,在失败时退出,下次运行时从失败的用例开始

pytest -sw
pytest --stepwise

--stepwise-skip

跳过第一个失败的test,如果再遇到失败就退出

pytest --stepwise-skip

--allure-severities=SEVERITIES_SET

指定allure severities运行

pytest --allure-severities=blocker, critical, normal, minor, trivial

--allure-epics=EPICS_SET

指定allure epics运行

pytest --allure-epics=my_epic

--allure-features=FEATURES_SET

指定allure features运行

pytest --allure-features=my_feature

--allure-stories=STORIES_SET

指定allure stories运行

pytest --allure-stories=my_story

--allure-link-pattern=LINK_TYPE:LINK_PATTERN

不知道怎么用,溜了溜了

pytest --allure-link-pattern=

3.reporting

--durations=N

显示2个最慢的setup/test的耗时

pytest --durations=2

显示所有耗时

pytest --durations=0

-v, --verbose

输出详细信息

pytest -v
pytest --verbose

-q, --quiet

输出简要信息

pytest -q
pytest --quiet

--verbosity=VERBOSE

设置信息显示等级为2

pytest --verbosity=2

-r chars

默认"fE"

显示failed的信息

pytest -r f

显示Error的信息

pytest -r E

显示skipped的信息

pytest -r s

显示xfailed的信息

pytest -r x

显示Xpassed的信息

pytest -r X

显示passed的信息

pytest -r p

显示Passed with output的信息

pytest -r P

显示all except passed的信息

pytest -r a
pytest -r A

显示warnings are enabled by default (--disable-warnings)的信息

pytest -r w

重置list

pytest -r N

-l, --showlocals

在tracebacks中显示局部变量,默认不显示

pytest -l
pytest --showlocals

--tb=style

traceback打印模式

pytest --tb=auto
pytest --tb=long
pytest --tb=short
pytest --tb=line
pytest --tb=native
pytest --tb=no

--show-capture

失败的tests如何显示,默认"all"

pytest --show-capture=no
pytest --show-capture=stdout
pytest --show-capture=stderr
pytest --show-capture=log
pytest --show-capture=all

--full-trace

不截取traceback,默认会截断

pytest --full-trace

--color=color

显示颜色

pytest --color=yes

不显示颜色

pytest --color=no

自动

pytest --color=auto

--pastebin=mode

没什么用

pytest --pastebin=mode

--junit-xml=path

创建junit-xml风格的测试报告

pytest --junit-xml=path

--junit-prefix=str

junit-xml输出中的classnames添加前缀hello

pytest --junit-prefix="hello"

--result-log=path

不建议使用

pytest --result-log=path

4.collection

--collect-only, --co

只收集,不执行。可以用来统计写了多少条自动化用例

pytest --collect-only
pytest --co

--pyargs

把所有参数解释为python包(package)

pytest --pyargs

--ignore=path

忽略不收集,可以多个(逗号隔开)

pytest --ignore=path1,path2,path3

--ignore-glob=path

path匹配,可以多个(逗号隔开)

pytest --ignore-glob="*_01.py"

--deselect=nodeid_prefix

通过node id prefix反选。可以多个(逗号隔开)

取消选择tests/foobar/test_foobar_01.py::test_a

--deselect="tests/foobar/test_foobar_01.py::test_a"

--confcutdir=dir

只加载相对于tests/foobar/目录的conftest.py文件

pytest --confcutdir="tests/foobar/"

--noconftest

不加载conftest.py文件

pytest --noconftest

--keep-duplicates

收集重复的test文件,默认只会收集1item,加参数后会收集2items

pytest test.py test.py --keep-duplicates

--collect-in-virtualenv

收集本地虚拟环境目录的tests

pytest --collect-in-virtualenv

--doctest-modules

doctest没啥用

pytest --doctest-modules

--doctest-report={none,cdiff,ndiff,udiff,only_first_failure}

doctest没啥用

pytest --doctest-report={none,cdiff,ndiff,udiff,only_first_failure}

--doctest-glob=pat

doctest没啥用

pytest --doctest-glob=pat

--doctest-ignore-import-errors

doctest没啥用

pytest --doctest-ignore-import-errors

--doctest-continue-on-failure

doctest没啥用

pytest --doctest-continue-on-failure

5.test session debugging and configuration

--basetemp=dir

test run的base临时目录(如果存在会先删除)

pytest --basetemp=dir

-V, --version

pytest版本

pytest -V
pytest --version

-h, --help

pytest帮助

pytest -h
pytest --help

-p name

加载plugin module或 entry point

pytest -p name

不加载doctest

pytest -p no:doctest

--trace-config

查看本地安装好的第三方插件

pytest --trace-config

--debug

保存debug信息到'pytestdebug.log'文件

pytest --debug

-o OVERRIDE_INI, --override-ini=OVERRIDE_INI

覆盖ini文件配置

pytest -o xfail_strict=True -o cache_dir=cache
pytest --override-ini=OVERRIDE_INI

--assert=MODE

默认rewrite

pytest --assert=rewrite

无assertion debugging

pytest --assert=plain

--setup-only

只setup fixtures,不执行tests

pytest --setup-only

--setup-show

执行tests的时候显示fixture setup

pytest --setup-show

--setup-plan

显示fixtures和tests计划会执行什么,但是不执行

也可以用来统计自动化用例

pytest --setup-plan

6.pytest-warnings

-W PYTHONWARNINGS, --pythonwarnings=PYTHONWARNINGS

设置报告哪些warnings

pytest -W PYTHONWARNINGS
pytest --pythonwarnings=PYTHONWARNINGS

7.logging

推荐直接使用loguru第三方库。

--log-level=LEVEL

默认没有设置,依赖log handler

WARNING DEBUG INFO ERROR

pytest --log-level=LEVEL

--log-format=LOG_FORMAT

日志格式

pytest --log-format="%(asctime)s %(levelname)s %(message)s"

--log-date-format=LOG_DATE_FORMAT

日期格式

pytest --log-date-format="%Y-%m-%d %H:%M:%S"

--log-cli-level=LOG_CLI_LEVEL

cli日志级别

pytest --log-cli-level=LOG_CLI_LEVEL

--log-cli-format=LOG_CLI_FORMAT

cli日志格式

pytest --log-cli-format="%(asctime)s %(levelname)s %(message)s"

--log-cli-date-format=LOG_CLI_DATE_FORMAT

cli日志级别

pytest --log-cli-date-format="%Y-%m-%d %H:%M:%S"

--log-file=LOG_FILE

日志文件路径

pytest --log-file=LOG_FILE

--log-file-level=LOG_FILE_LEVEL

日志文件级别

pytest --log-file-level=LOG_FILE_LEVEL

--log-file-format=LOG_FILE_FORMAT

日志文件格式

pytest --log-file-format="%(asctime)s %(levelname)s %(message)s"

--log-file-date-format=LOG_FILE_DATE_FORMAT

日志文件日期

pytest --log-file-date-format="%Y-%m-%d %H:%M:%S"

--log-auto-indent=LOG_AUTO_INDENT

自动换行

true|flase on|off

pytest --log-auto-indent=LOG_AUTO_INDENT

8.reporting-allure

--alluredir=DIR

allure数据生成目录,注意不是html哦,而是json文件,需要allure generate data_dir -o html_dir才能生成html

pytest --alluredir=DIR

--clean-alluredir

如果存在alluredir,先清除

pytest --clean-alluredir

--allure-no-capture

报告不捕获pytest的logging/stdout/stderr信息

pytest --allure-no-capture

9.ini-options

ini文件用例设置一些初始化默认值。

部分内容其实质跟参数是一样用法。

markers (linelist)

自定义marker

# pytest.ini
[pytest] markers =
webtest: Run the webtest case
hello: Run the hello case

empty_parameter_set_mark (string)

默认情况下,如果@pytest.mark.parametrizeargnames中的参数没有接收到任何的实参的话,用例的结果将会被置为SKIPPED;empty_parameter_set_mark可以设置为skip、xfail、fail_at_collect。

norecursedirs (args)

忽略一些目录

# pytest.ini

[pytest]

norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src

testpaths (args)

指定目录

# pytest.ini

[pytest]

testpaths = test_path

usefixtures (args)

默认使用fixtures。

python_files (args)

glob文件匹配模式的python test modules。

python_classes (args)

前缀/glob文件匹配模式的python test classes。

python_functions (args)

前缀/glob文件匹配模式的python test functions。

ty_support (bool)

有风险,没用。

console_output_style (string)

控制台输出样式

  • classic 经典样式
  • progress: 带进度百分比
  • count 计数而不是百分比

xfail_strict (bool)

默认false,true时@pytest.mark.xfail的test,会被强制失败,即使是成功的。

enable_assertion_pass_hook (bool)

确保删除之前生成的pyc缓存文件。

junit_suite_name (string)

不用学。

junit_logging (string)

不用学。

junit_log_passing_tests (bool)

不用学。

junit_duration_report (string)

不用学。

junit_family (string)

不用学。

doctest_optionflags (args)

不用学。

doctest_encoding (string)

不用学。

cache_dir (string)

缓存目录。

filterwarnings (linelist)

同 -W/--pythonwarnings。

log_level (string)

同命令行参数。

log_format (string)

同命令行参数。

log_date_format (string)

同命令行参数。

log_cli (bool)

true,test run的时候,实时显示日志。

log_cli_level (string)

同命令行参数。

log_cli_format (string)

同命令行参数。

log_cli_date_format (string)

同命令行参数。

log_file (string)

同命令行参数。

log_file_level (string)

同命令行参数。

log_file_format (string)

同命令行参数。

log_file_date_format (string)

同命令行参数。

log_auto_indent (string)

同命令行参数。

faulthandler_timeout (string)

如果test的运行时间超过设定的时间(超时),会打印相关traceback。

addopts (args)

执行时带的默认参数,可以避免每次都要输入一遍

addopts = -rsxX -v --reruns=1 --count=2

minversion (string)

pytest最小版本号。如果pytest低于这个版本,运行会报错。

required_plugins (args)

必须的插件。

10.environment variables

PYTEST_ADDOPTS

命令行选项

export PYTEST_ADDOPTS=

PYTEST_PLUGINS

包含应作为插件加载的以逗号分隔的模块列表

export PYTEST_PLUGINS=mymodule.plugin,xdist

PYTEST_DISABLE_PLUGIN_AUTOLOAD

禁用插件自动加载

export PYTEST_DISABLE_PLUGIN_AUTOLOAD=

PYTEST_DEBUG

启用pytest调试

export PYTEST_DEBUG=

版权申明:本文为博主原创文章,转载请保留原文链接及作者。

如果您喜欢我写的文章,请关注公众号支持一下,谢谢哈哈哈。

pytest封神之路第二步 132个命令行参数用法的更多相关文章

  1. pytest封神之路第零步 快速入门

    背景:本文是在系列第五篇发表后的补充篇章,第一篇介绍了tep,可能对不熟悉pytest的朋友不够友好,特意补充入门篇,帮大家快速了解如何动手写pytest.如果你是从这篇文章第一次阅读,那么请忽略以上 ...

  2. pytest封神之路第五步 参数化进阶

    用过unittest的朋友,肯定知道可以借助DDT实现参数化.用过JMeter的朋友,肯定知道JMeter自带了4种参数化方式(见参考资料).pytest同样支持参数化,而且很简单很实用. 语法 在& ...

  3. pytest封神之路第一步 tep介绍

    『 tep is a testing tool to help you write pytest more easily. Try Easy Pytest! 』 tep前身 tep的前身是接口自动化测 ...

  4. pytest封神之路第四步 内置和自定义marker

    可以通过命令行查看所有marker,包括内置和自定义的 pytest --markers 内置marker 内置marker本文先讲usefixtures .filterwarnings .skip ...

  5. Python测试框架pytest命令行参数用法

    在Shell执行pytest -h可以看到pytest的命令行参数有这10大类,共132个 序号 类别 中文名 包含命令行参数数量 1 positional arguments 形参 1 2 gene ...

  6. Pytest 学习(二十五)- allure 命令行参数【转】

    先看看 allure 命令的帮助文档 cmd 敲 allure -h allure 命令的语法格式 allure [options] [command] [command options] optio ...

  7. pytest封神之路第三步 精通fixture

    首先放一句"狠话". 如果你不会fixture,那么你最好别说自己会pytest. (只是为了烘托主题哈,手上的砖头可以放下了,手动滑稽) fixture是什么 看看源码 def ...

  8. pytest封神之路第六步 断言技巧

    pytest的断言把Python语言简洁的优点发挥的淋漓尽致,因为它用的就是Python的标准断言assert. assert基础 assert用法 assert_stmt ::= "ass ...

  9. 《带你装B,带你飞》pytest成神之路2- 执行用例规则和pycharm运行的三种姿态

    1. 简介 今天北京下的雪好大好美啊!!!哎呀,忘记拍照片了,自己想象一下吧.言归真传,今天还是开始pytest的学习和修炼,上一篇写完后群里反响各式各样的,几家欢乐几家愁,有的高兴说自己刚好要用到了 ...

随机推荐

  1. [学习笔记] Numpy基础 系统学习

    [学习笔记] Numpy基础 上专业选修<数据分析程序设计>课程,老师串讲了Numpy基础,边听边用jupyter敲了下--理解+笔记. 老师讲的很全很系统,有些点没有记录,在PPT里就不 ...

  2. 全面介绍eBPF-概念

    全面介绍eBPF-概念 前面介绍了BCC可观测性和BCC网络,但对底层使用的eBPF的介绍相对较少,且官方欠缺对网络方面的介绍.下面对eBPF进行全面介绍. 目录 全面介绍eBPF-概念 BPF概述 ...

  3. Deep learning-based personality recognition from text posts of online social networks 阅读笔记

    文章目录 一.摘要 二.模型过程 1.文本预处理 1.1 文本切分 1.2 文本统一 2. 基于统计的特征提取 2.1 提取特殊的语言统计特征 2.2 提取基于字典的语言特征 3. 基于深度学习的文本 ...

  4. 花式求解 LeetCode 279题-Perfect Squares

    原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...

  5. Jdk1.6 HTTPS访问问题解决办法

    真是艹蛋的一次经历,jdk6上面去访问别人的https,还好有百度搞定了问题.现在写下随笔,记录下; 首先要自己重写SSLSocketFactory这个类, 下面是自己重写的这个类:TLSSocket ...

  6. Bootstrap4-思维导图-知识点总结

    Bootstrap4-思维导图-知识点总结 By:Mirror王宇阳 time:2020/4/30 有错误之处 烦请见谅!

  7. 经典的 Fork 炸弹解析

    原文出处: saymagic Jaromil 在 2002 年设计了最为精简的一个Linux Fork炸弹,整个代码只有13个字符,在 shell 中运行后几秒后系统就会宕机: ::(){:|:&am ...

  8. JDBC理论知识

    JDBC理论知识 JDBC基础 JDBC(Java Database Connectivity)是一个独立于特定数据库管理系统.通用的SQL数据库存取和操作的公共接口(一组API), 定义了用来访问数 ...

  9. 团队作业4:第四篇Scrum冲刺博客(歪瑞古德小队)

    目录 一.Daily Scrum Meeting 1.1 会议照片 1.2 项目进展 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3.2 Code Review 记录 3.3 issue ...

  10. 2020.5.26 第六篇 Scrum冲刺博客

    Team:银河超级无敌舰队 Project:招新通 项目冲刺集合贴:链接 目录 一.每日站立会议 1.1 会议照片 1.2 项目完成情况 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3. ...