pytest详解-(4)执行case的方法


执行指定case:

  • pytest.main()    #执行所有case
  • pytest.main(['-vs', 'TestCalc.py::TestCalc'])    #执行TestCalc.py文件内TestCalc类下所有case
  • pytest.main(['-vs', 'TestCalc.py::TestCalc::test_division_seven'])    #执行TestCalc.py文件内TestCalc类下名字为test_division_seven的case
  • pytest.main(['参数','xxx'])    pytest.main()内必须是list格式,在低版本已不支持str

具体参数:

  • -v:                                                  #打印详细运行的日志信息
  • -s:     pytest -s xxx.py                   #输出case中print的内容
  • -m:    pytest -m “tag名称”         #运行指定tag名称的用例,也就是运行有@pytest.mark.[标记名]这个标记的case
  • -k:     pytest -k “类名、方法名、类名 and not 方法名”         #运行指定case的用例
  • -x:                                                   #遇到失败的case就是停止执行
  • --lf:                                                   #只重新运行上次运行失败的用例(或如果没有失败的话会全部跑)
  • --ff:                                                   #运行所有测试,但首先运行上次运行失败的测试(这可能会重新测试,从而导致重复的fixture setup/teardown)
  • --maxfail=num:                          #当用例失败个数达到num时,停止运行
  • --collect-only:   收集测试用例,展示出哪些用例会被执行(只是展示不会执行case) 
  • --junit-xml:--junit-xml=path/name.xml        #在指定目录或当前目录下生成xml格式的报告(需要在pytest.ini文件内声明格式:junit_family=xunit2)
  • --steup-show                                #完整展示每个用例的fixture调用顺序

注意:必须在pytest.ini文件内声明标签,不然则会告警PytestUnknownMarkWarning

 

1 #demo:
2 pytest.ini
3 markers = 
4          div
5          tag

 命令行执行:

pytest test_quick_start.py --junit-xml=report.xml

main执行:
pytest.main(["-s", "TestCalc.py", "-m", "div", "--junit-xml=report.xml"])
pytest.main(["-vsx", "TestCalc.py", "-m", "div"])

语法:

@pytest.mark.smoke
@pytest.mark.get
$pytest -m 'smoke'
  仅运行标记smoke的函数
$pytest -m 'smoke and get'
  运行标记smoke和get的函数
$pytest -m 'smoke or get'
  运行标记smoke或get的函数
$pytest -m 'smoke and not get'
    运行标记smoke和标记不是get的函数


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM