pytes命令行参数

在cmd or xshell中运行pytest –h 可以查询出所有的命令行参数

用法:pytest [options] [file_or_dir] [file_or_dir] [...]

分类 命令 意思 举例  

positional arguments

file_or_dir

指定一个或多个文件目录

 

 

general

-k EXPRESSION

使用表达式指定希望运行的测试用例

 

 

-m MARKEXPR

用于标记测试并分组,以便快速选中并运行

 

 

–continue-on-collection-errors


收集test失败,仍然强制继续执行 pytest --continue-on-collection-errors  
  –lf, --last-failed 重跑上次失败的tests,如果没有失败就重跑全部

pytest -lf

pytest --last-failed

 
  –lfnf={all,none}, --last-failed-no-failures={all,none} 没有last-failed缓存数据,或上次没有失败时,跑tests

跑全部的:pytest --lfnf=all

不运行:pytest --lfnf=none

--last-failed --lfnf=none 一般结合使用

表示重批上次失败的脚本,若有没有失败的,就不允许了

 

--markers

显示 脚本中内置的一些标记

 

 

 

-x, --exitfirst

第一个error或failed的test就退出

 

 

 

--maxfail=num

指定遇到num个error就退出

pytest --maxfail=2      2个errors或failures就退出

 

 

–strict-config

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

pytest --strict-config

 
 

-c file


my.ini文件加载配置 pytest -c my.ini  
 

–rootdir=ROOTDIR


定义测试的根目录

pytest --rootdir="root_dir"

可以是相对路径:'root_dir','./ root_dir','root_dir / another_dir /';
绝对路径:“ / home / user / root_dir”;

路径与变量:“ $ HOME / root_dir”

 
  –fixtures, --funcargs 显示fixtures pytest --fixtures  
  –fixtures-per-test 显示每个test用到的fixture pytest --fixtures-per-test  
  --import-mode={prepend,append}      
  –pdb 在errors或KeyboardInterrupt时,启用默认Python debugger pytest --pdb  
  –pdbcls=modulename:classname 启用自定义Python debugger pytest --pdbcls=IPython.terminal.debugger:TerminalPdb  
  –capture=method 文件描述符(默认) pytest --capture=fd  
  -s 等价于--capture=no    
  –runxfail 强制运行xfail标记的test    
  –ff, --failed-first 优先跑上次失败的test,tests的顺序会被打乱 pytest --failed-first  
  –nf, --new-first 优先跑新添加的tests,剩余的按文件mtime顺序 pytest --new-first  
  –cache-show=[CACHESHOW] 显示缓存

显示缓存,默认*: pytest --cache-show

显示缓存,带参数cache\nodeids:pytest --cache-show=cache\nodeids

 
 

–cache-clear

运行开始时清缓存 pytest --cache-clear  
  --allure_severities=SEVERITIES_SET 指定serverities运行    
  --allure_features=FEATURES_SET 指定feature运行 pytest ./case --allure_features="SiteService"  
  --allure_stories=STORIES_SET 执行story运行    
  –trace run每个test时break,进入debugger交互 pytest --trace  
reporting-v, --verbose输出详细信息pytest -v

  -q, --quiet 输出简要信息    
  –verbosity=VERBOSE 设置信息显示等级为

设置信息显示等级为2:pytest --verbosity=2

 
  -r chars 显示字符指定的额外摘要信息

(f)失败,(E)错误,(s)跳过,(x)失败,(X)通过

pytest -r f

pytest -r chars   默认"fE"

 
  -l, --showlocals 在tracebacks中显示局部变量,默认不显示    
  –tb=style traceback打印模式    
  --show-capture={no,stdout,stderr,log,all} 失败的tests如何显示,默认"all" pytest --show-capture=no  
  –full-trace 不截取traceback,默认会截断    
  –color=color 显示颜色

(yes/no/auto)

显示颜色:pytest --color=yes

 
  –pastebin=mode 将失败的所有信息发送到bpaste.net pastebin服务    
  –junit-xml=path 创建junit-xml风格的测试报告    
  –junit-prefix=str junit-xml输出中的classnames添加前缀 pytest --junit-prefix="hello"  
  –result-log=path 机器可读结果日志的DEPRECATED路径。    

reporting-allure

–alluredir=DIRallure数据生成目录,注意不是html哦,而是json文件,需要allure generate data_dir -o html_dir才能生成htmlpytest --alluredir=DIR 
  –clean-alluredir 如果存在alluredir,先清除 pytest --clean-alluredir  
  –allure-no-capture 报告不捕获pytest的logging/stdout/stderr信息    
         
collection

–collect-only, --co

只收集,不执行。可以用来统计写了多少条自动化用例pytest --collect-only 
  –pyargs 把所有参数解释为python包(package)    
  –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 加载指定目录的confitest.py文件

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

pytest --confcutdir="tests/foobar/"
 
  –noconftest 不加载conftest.py文件    
  –keep-duplicates 收集重复的test文件,默认只会收集1item,加参数后会收集2items pytest test.py test.py --keep-duplicates  
 
–collect-in-virtualenv
收集本地虚拟环境目录的tests pytest --collect-in-virtualenv  
 

–doctest-modules

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

–doctest-glob=pat

–doctest-ignore-import-errors

–doctest-continue-on-failure

没啥用    

 

详细解释

1 file_or_dir指定一个或多个文件目录

可以指定到文件夹—表示运行这个文件夹下的所有测试脚本

也可以指定到具体的文件—表示运行这个文件中的测试脚本

pytest E:\PyCharm 2017.2.4\preProject\script\case

pytest E:\PyCharm 2017.2.4\preProject\script\case\test_file.py

 

2 -k EXPRESSION使用表达式指定希望运行的测试用例

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

pytest -k "test_method or test_other"

名字不包含test_method

pytest -k "not test_method"

名字不包含test_method不包含test_other

pytest -k 'not test_method and not test_other'

大小写敏感。

3 -m MARKEXPR 用于标记测试并分组,以便快速选中并运行

以test_replace()和test_member_access()为例,它们不在同一个文件里,如果希望同时运行它们,那么可以预先做好标记。

  这里使用什么标记名由你自己决定,比如使用run_these_case,则可以用@pytest.mark.run_these_case这样的装饰器(decorator)来标记,如下所示:

  import pytest ...

  @pytest.mark.run_these_case

  def test_member_access():

  ...  

  给test_replace()也做上同样的标记。有相同标记的测试用例,可以一起运行。使用pytest -m run_these_case命令就可以同时运行test_replace()和test_member_access()。

4 –markers 显示markers

脚本中做了一些标记,想知道有哪些,可以用这个显示出来

比如在脚本中做了这样的装饰器来标记 @pytest.mark.run_these_case,运行之后就会显示出来

5 -x, --exitfirst第一个error或failed的test就退出

正常情况下,pytest会运行每一个搜索到的测试用例。如果某下测试函数被断言败,或者触发了外部异常,则该测试用例的运行就会到此为止,pytest将其标记为失败后会继续运行下一个测试用例。通常,这是我们期望的运行模式。但如果希望在遇到失败时立即停止整个会话,这时-x选项就派上用场了。

 

 

 

5.test session debugging and configuration

–basetemp=dir

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

pytest --basetemp=dir

  • 1

-V, --version

pytest版本

pytest -V

  • 1

pytest --version

  • 1

-h, --help

pytest帮助

pytest -h

  • 1

pytest --help

  • 1

-p name

加载plugin module或 entry point

pytest -p name

  • 1

不加载doctest

pytest -p no:doctest

  • 1

–trace-config

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

pytest --trace-config

  • 1

–debug

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

pytest --debug

  • 1

-o OVERRIDE_INI, --override-ini=OVERRIDE_INI

覆盖ini文件配置

pytest -o xfail_strict=True -o cache_dir=cache

  • 1

pytest --override-ini=OVERRIDE_INI

  • 1

–assert=MODE

默认rewrite

pytest --assert=rewrite

  • 1

无assertion debugging

pytest --assert=plain

  • 1

–setup-only

只setup fixtures,不执行tests

pytest --setup-only

  • 1

–setup-show

执行tests的时候显示fixture setup

pytest --setup-show

  • 1

–setup-plan

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

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

pytest --setup-plan

  • 1

6.pytest-warnings

-W PYTHONWARNINGS, --pythonwarnings=PYTHONWARNINGS

设置报告哪些warnings

pytest -W PYTHONWARNINGS

  • 1

pytest --pythonwarnings=PYTHONWARNINGS

  • 1

7.logging

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

–log-level=LEVEL

默认没有设置,依赖log handler

WARNING DEBUG INFO ERROR

pytest --log-level=LEVEL

  • 1

–log-format=LOG_FORMAT

日志格式

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

  • 1

–log-date-format=LOG_DATE_FORMAT

日期格式

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

  • 1

–log-cli-level=LOG_CLI_LEVEL

cli日志级别

pytest --log-cli-level=LOG_CLI_LEVEL

  • 1

–log-cli-format=LOG_CLI_FORMAT

cli日志格式

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

  • 1

–log-cli-date-format=LOG_CLI_DATE_FORMAT

cli日志级别

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

  • 1

–log-file=LOG_FILE

日志文件路径

pytest --log-file=LOG_FILE

  • 1

–log-file-level=LOG_FILE_LEVEL

日志文件级别

pytest --log-file-level=LOG_FILE_LEVEL

  • 1

–log-file-format=LOG_FILE_FORMAT

日志文件格式

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

  • 1

–log-file-date-format=LOG_FILE_DATE_FORMAT

日志文件日期

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

  • 1

–log-auto-indent=LOG_AUTO_INDENT

自动换行

true|flase on|off

pytest --log-auto-indent=LOG_AUTO_INDENT

  • 1

  • 1

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

  • 1

minversion (string)

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

required_plugins (args)

必须的插件。

10.environment variables

PYTEST_ADDOPTS 命令行选项

export PYTEST_ADDOPTS=

  • 1

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

export PYTEST_PLUGINS=mymodule.plugin,xdist

  • 1

PYTEST_DISABLE_PLUGIN_AUTOLOAD 禁用插件自动加载

export PYTEST_DISABLE_PLUGIN_AUTOLOAD=

  • 1

PYTEST_DEBUG 启用pytest调试

export PYTEST_DEBUG=

posted @ 2021-03-12 16:11  溪棱  阅读(400)  评论(0编辑  收藏  举报