欢迎访问www.allbetgaming.com!

首页科技正文

忻州长途汽车站:Pytest系列(14)- 配置文件pytest.ini的详细使用

admin2020-04-1652

若是你还想重新学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

 

前言

pytest设置文件可以改变pytest的运行方式,它是一个牢固的文件pytest.ini文件,读取设置信息,按指定的方式去运行

 

非test文件

pytest内里有些文件是非test文件

  • pytest.ini:pytest的主设置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture设置
  • _init_.py:识别该文件夹为python的package包

 

查看pytest.ini的设置选项

cmd执行

pytest --help

 

找到这部分内容

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:

  markers (linelist):   markers for test functions
  empty_parameter_set_mark (string):
                        default marker for empty parametersets
  norecursedirs (args): directory patterns to avoid for recursion
  testpaths (args):     directories to search for tests when no files or directories are given in the command line.
  usefixtures (args):   list of default fixtures to be used with this project
  python_files (args):  glob-style file patterns for Python test module discovery
  python_classes (args):
                        prefixes or glob names for Python test class discovery
  python_functions (args):
                        prefixes or glob names for Python test function and method discovery
  disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
                        disable string escape non-ascii characters, might cause unwanted side effects(use at your own
                        risk)
  console_output_style (string):
                        console output: "classic", or with additional progress information ("progress" (percentage) |
                        "count").
  xfail_strict (bool):  default for the strict parameter of xfail markers when not given explicitly (default: False)
  enable_assertion_pass_hook (bool):
                        Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache
                        files.
  junit_suite_name (string):
                        Test suite name for JUnit report
  junit_logging (string):
                        Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|all
  junit_log_passing_tests (bool):
                        Capture log information for passing tests to JUnit report:
  junit_duration_report (string):
                        Duration time to report: one of total|call
  junit_family (string):
                        Emit XML for schema: one of legacy|xunit1|xunit2
  doctest_optionflags (args):
                        option flags for doctests
  doctest_encoding (string):
                        encoding used for doctest files
  cache_dir (string):   cache directory path.
  filterwarnings (linelist):
                        Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
  log_print (bool):     default value for --no-print-logs
  log_level (string):   default value for --log-level
  log_format (string):  default value for --log-format
  log_date_format (string):
                        default value for --log-date-format
  log_cli (bool):       enable log display during test run (also known as "live logging").
  log_cli_level (string):
                        default value for --log-cli-level
  log_cli_format (string):
                        default value for --log-cli-format
  log_cli_date_format (string):
                        default value for --log-cli-date-format
  log_file (string):    default value for --log-file
  log_file_level (string):
                        default value for --log-file-level
  log_file_format (string):
                        default value for --log-file-format
  log_file_date_format (string):
                        default value for --log-file-date-format
  log_auto_indent (string):
                        default value for --log-auto-indent
  faulthandler_timeout (string):
                        Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Not
                        available on Windows.
  addopts (args):       extra command line options
  minversion (string):  minimally required pytest version
  rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.
  rsyncignore (pathlist):
                        list of (relative) glob-style paths to be ignored for rsyncing.
  looponfailroots (pathlist):
                        directories to check for changes

 

pytest.ini应该放那里?

就放在项目根目录下 ,不要乱放,不要乱起其他名字

 

接下来讲下常用的设置项

 

marks

作用:测试用例中添加了 @pytest.mark.webtest 装饰器,若是不添加marks选项的话,就会报warnings

花样:list列表类型

写法:

[pytest]
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

 

xfail_strict

作用:设置xfail_strict = True可以让那些符号为@pytest.mark.xfail但现实通过显示XPASS的测试用例被讲述为失败

花样:True 、False(默认),1、0

写法:

[pytest]

# mark符号说明
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

 

详细代码栗子

未设置 xfail_strict = True 时,测试效果显示XPASS

@pytest.mark.xfail()
def test_case1():
    a = "a"
    b = "b"
    assert a != b

collecting ... collected 1 item

02断言异常.py::test_case1 XPASS [100%]

============================= 1 xpassed in 0.02s ==============================

 

已设置 xfail_strict = True 时,测试效果显示failed

collecting ... collected 1 item

02断言异常.py::test_case1 FAILED                                         [100%]
02断言异常.py:54 (test_case1)
[XPASS(strict)] 

================================== FAILURES ===================================
_________________________________ test_case1 __________________________________
[XPASS(strict)] 
=========================== short test summary info ===========================
FAILED 02断言异常.py::test_case1
============================== 1 failed in 0.02s ==============================

 

addopts

作用:addopts参数可以更改默认下令行选项,这个当我们在cmd输入一堆指令去执行用例的时刻,就可以用该参数取代了,省去重复性的敲下令事情

好比:想测试完天生讲述,失败重跑两次,一共运行两次,通过分布式去测试,若是在cmd中写的话,下令会很长

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

 

每次都这样敲不太现实,addopts就可以完善解决这个问题

[pytest]

# mark
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

# 下令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!

 

log_cli

作用:控制台实时输出日志

花样:log_cli=True 或False(默认),或者log_cli=1 或 0

log_cli=0的运行效果

 

log_cli=1的运行效果

结论

很明显,加了log_cli=1之后,可以清晰看到哪个package下的哪个module下的哪个测试用例是否passed照样failed;

以是平时测试代码是否有问题的情形下推荐加!!!但若是拿去批量跑测试用例的话不建议加,谁知道会不会影响运行性能呢?

 

norecursedirs

作用:pytest 网络测试用例时,会递归遍历所有子目录,包罗某些你明知道没必要遍历的目录,遇到这种情形,可以使用 norecursedirs 参数简化 pytest 的搜索事情【照样挺有用的!!!】

默认设置: norecursedirs = .* build dist CVS _darcs {arch} *.egg 

准确写法:多个路径用空格离隔

[pytest]

norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util

 

更改测试用例网络规则

pytest默认的测试用例网络规则

  • 文件名以 test_*.py 文件和 *_test.py
  • 以  test_ 开头的函数
  • 以  Test 开头的类,不能包罗 __init__ 方式
  • 以  test_ 开头的类内里的方式

 

我们是可以修改或者添加这个用例网络规则的;固然啦,是建议在原有的规则上添加的,如下设置

[pytest]

python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*

 

 ,

Sunbet

Sunbet www.ningyanganews.com Sunbet以著名的服务态度及优秀的网络环境,Sunbet客服24小时在线让你玩得过瘾,赢得开心。

转载声明:本站发布文章及版权归原作者所有,转载本站文章请注明文章来源!

本文链接:https://www.juneview.com/post/710.html

网友评论

最新文章

最新评论

  • 联博统计 10/26 说:

    欧博APP下载欢迎进入欧博APP下载(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。围观一下大佬

  • 联博统计 10/26 说:

    欧博APP下载欢迎进入欧博APP下载(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。围观一下大佬

  • 联博统计接口 10/25 说:

    欧博亚洲官方注册欢迎进入欧博亚洲官方注册(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。这反转很溜

  • allbet登陆官网 10/24 说:

    ALLBET官网娱乐平台开户欢迎进入ALLBET官网娱乐平台开户:www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。看得出来很用心

  • UG环球官方注册 10/24 说:

    联博统计接口www.326681.com采用以太坊区块链高度哈希值作为统计数据,联博以太坊统计数据开源、公平、无任何作弊可能性。联博统计免费提供API接口,支持多语言接入。我是看文小能手

  • UG环球网址 10/24 说:

    AllbetGmaing下载欢迎进入AllbetGmaing下载(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。想象力不错

  • UG环球网址 10/24 说:

    联博以太坊www.326681.com采用以太坊区块链高度哈希值作为统计数据,联博以太坊统计数据开源、公平、无任何作弊可能性。联博统计免费提供API接口,支持多语言接入。美中不足,有点短

  • ALLBET官网开户网址 10/24 说:

    欧博亚洲APP下载欢迎进入欧博亚洲APP下载(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。看得很心潮澎湃