美文网首页
pytest基本使用

pytest基本使用

作者: 社会主义顶梁鹿 | 来源:发表于2021-07-09 15:21 被阅读0次

    安装pytest

    pip install -U pytest

    pytest --version    #   验证安装的版本

    pytest测试用例规则

    (1)测试文件以test_开头(以_test结尾也可以)

    (2)测试类以Test开头,并且不能带有 init 方法

    (3)测试函数以test_开头

    (4)断言使用基本的assert即可

    运行模式

    1.运行后生成测试报告(htmlReport)

    安装pytest-html

    pip install -U pytest-html

    运行模式

    pytest --html=report.html

    2.运行指定的case

    模式1:直接运行test_se.py文件中的所有cases

    pytest test_se.py

    模式2:运行test_se.py文件中的TestClassOne这个class下的cases

    pytest test_se.py::TestClassOne

    模式3:运行test_se.py文件中的TestClassTwo这个class下的test_one

    pytest test_se.py::TestClassTwo::test_one

    3.多进程运行cases

    当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长,就可以用多进程来运行。

    安装pytest-xdist

    pip install -U pytest-xdist

    运行模式

    pytest test_se.py -n NUM

    其中NUM填写并发的进程数。

    4.重试运行cases

    在做接口测试时,有事会遇到503或短时的网络波动,导致case运行失败,而这并非是我们期望的结果,此时可以就可以通过重试运行cases的方式来解决

    安装pytest-rerunfailures

    pip install -U pytest-rerunfailures

    运行模式

    pytest test_se.py --reruns NUM

    NUM填写重试的次数。

    5.显示print内容

    在运行测试脚本时,为了调试或打印一些内容,我们会在代码中加一些print内容,但是在运行pytest时,这些内容不会显示出来。如果带上-s,就可以显示了。

    运行模式

    pytest test_se.py -s

    pytest的多种运行模式是可以叠加执行的,比如说,你想同时运行4个进程,又想打印出print的内容。可以用:

    pytest test_se.py -s -n 4

    相关文章

      网友评论

          本文标题:pytest基本使用

          本文链接:https://www.haomeiwen.com/subject/llpwhltx.html