• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

python:pytest相关常用插件和pytest.ini文件配置

武飞扬头像
天青色是₩
帮助1

1、pytest日常测试时常用插件及命令

具体命令使用方法及规则请跳转至:http://t.csdn.cn/Wsc7R

pytest-xdist 使用多线程执行用例 使用:-n 3 或者 “-n=2"

pytest-rerunfailures 用例失败重跑 使用:--reruns 3 或者 ”--reruns=3"

pytest-html 生成html报告 使用:--html=report.html

pytest-ordering 改变测试用例执行顺序,使用:@pytest.mark.run(order=1)

allure-pytest 生成allure级别的测试报告(需要下载allure文件) 使用:--alluredir=report & os.system(allure.bat serve report)

2、pytest.ini文件配置

pytest.ini 文件是pytest运行的主配置文件,可以改变pytest运行的默认行为,一般放在根目录下,格式如下:

  1.  
    [pytest]
  2.  
    addopts = -vs
  3.  
    testpaths = ./test_demo3.py
  4.  
     
  5.  
    python_files = test_* check_*
  6.  
    python_classes = Test* Check*
  7.  
    python_functions = test_* check_*
  8.  
     
  9.  
    markers =
  10.  
    smoke:maoyan

1、addopts:默认执行命令项 可以加一些命令,如同:pytest.main(["-vs","-n=2"])

  1. -v:未加前只打印模块名,加v后打印类名、模块名、方法名,显示更详细的信息
  2. -s:表示输出调试信息,用于显示测试函数中print()打印的信息
  3. -vs:这两个参数可以一起使用
  4. -q:表示只显示整体测试结果
  5. -n:支持多线程或者分布式运行测试用例(需安装:pytest-xdist插件)
  6. --reruns :用例失败重跑(需安装:pytest-rerunfailures插件)
  7. -m ”标记“ 说明:执行特定的测试用例,如 -m smoke
addopts = -vs -n 3 --reruns 2 -m smoke

2、testpaths:pytest执行的文件路径,例如:./testcase

testpaths = ./testcase/test_demo3.py

3、pytest执行时默认找文件、类、方法

     pytest 默认的用例收集规则:

  1. 测试模块必须以 test_ 开头或以 _test 结尾;
  2. 测试类必须以 Test 开头,且不能有 __init__() ;
  3. 测试方法必须以 test_ 开头;

上面的默认规则可以修改或者增加收集规则:

pytest_fails:执行文件的搜索的模块文件名

python_classes:执行类的类名

python_functions:执行方法的方法名

  1.  
    python_files = test_* check_*
  2.  
    python_classes = Test* Check*
  3.  
    python_functions = test_* check_*

这里增加了以 check开头的文件、类、方法,若有check开头也会被执行

4、markers:标记

  1.  
    markers =
  2.  
    smoke:maoyan

此时配置了smoke标记,当然标记可以添加很多

使用时在命令后加命令:-m smoke

当用例上面有 @pytest.mark.smoke 的装饰器就表示会执行此用例,其他用例不执行,例如:

  1.  
    @pytest.mark.smoke
  2.  
    def test_demo():
  3.  
    pass

5、当pytest.ini文件配置完成后,在文件中用 pytest.main()时就不需要加参数了,执行时会自动找pytest.ini文件里的配置规则去执行

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcfkfi
系列文章
更多 icon
同类精品
更多 icon
继续加载