Pytest框架 — 02、Pytest的基本使用

1、pytest安装

安装方法:
pip install pytest
安装验证:
pytest --version

2、pytest常用插件

上一篇说过pytest拥有丰富的第三方插件,我们可以根据需要进行选择,甚至可以自定义pytest插件
常用的插件如下:

  • pytest-html:生成html格式的自动化测试报告
  • pytest-rerunfailures:case执行失败重复执行
  • pytest-ordering:改变测试用例的执行顺序
  • allure-pytest:用于结合allure定制更美观的测试报告

pytest插件列表官网:https://docs.pytest.org/en/latest/reference/plugin_list.html

3、运行第一个例子

# 1、导入pytest
import pytest


# 2、编写一个函数
def add(a, b):
    return a + b

# 3、编写一个测试函数
def test_add():
    assert add(1, 2) == 3
    
"""
执行结果
Launching pytest with arguments first_demo.py::test_add --no-header --no-summary -q in /Users/qishuai/Desktop/笔记/web自动化测试/pytest/testPytest/demo

============================= test session starts ==============================
collecting ... collected 1 item

first_demo.py::test_add PASSED                                           [100%]

============================== 1 passed in 0.01s ===============================
"""

4、测试用例命名规则

Pytest可以在不同的函数、包中搜索测试用例,搜索规则如下:

  • 测试文件名以test_开头,或者以_test.py结尾
  • test开头的函数
  • test开头的方法
  • 测试类以Test开头,并且类中不能有__init__方法

注意:所有的包内必须有__init__.py文件

5、pytest的启动方式

(1)pycharm中直接在函数、方法或类上面右键执行

  1. pycharm中配置pytest框架的方法
    File -> Settings -> Tools -> Python Integrated Tools -> Testing

  2. 执行方法

(2)pytest主函数main下启动

demo目录

  1. 执行所有
if __name__ == '__main__':
    # 执行所有测试用例
    pytest.main()

"""
执行结果
collected 4 items

test_a.py .                                                              [ 25%]
test_b.py .                                                              [ 50%]
scripts/test_1.py .                                                      [ 75%]
scripts/test_2.py .                                                      [100%]

============================== 4 passed in 0.03s ===============================
"""
  1. 执行指定模块
import pytest

if __name__ == '__main__':
    pytest.main(['test_a.py'])

""" 
执行结果
collected 1 item

test_a.py .                                                              [100%]

============================== 1 passed in 0.01s ===============================
"""
  1. 执行指定目录
import pytest

if __name__ == '__main__':
    pytest.main(['./scripts'])

"""
执行结果
collected 2 items

scripts/test_1.py .                                                      [ 50%]
scripts/test_2.py .                                                      [100%]

============================== 2 passed in 0.02s ===============================
"""
  1. 执行指定函数或方法
import pytest

if __name__ == '__main__':
    # 路径与文件名用"/"分割,类,方法,函数用"::"分割
    pytest.main(['./scripts/test_1.py::test_1'])


"""
执行结果
collected 1 item

scripts/test_1.py .                                                      [100%]

============================== 1 passed in 0.01s ===============================
"""

(3)命令行模式

  1. 执行所有
pytest
py.test
  1. 执行指定模块
pytest -vs test_a.py
  1. 执行指定目录
pytest -vs ./scripts
  1. 执行指定函数或方法
pytest ./scripts/test_1.py::test_1

6、执行参数

在命令行执行和主函数执行时可以通过增加参数的方式来实现一些功能
eg:

pytest.main(['-vs'])
pytest -v -s xxx.py

常用参数:

  • -s:在测试结果中显示用例中print的内容
  • -v:更详细的展示测试结果,包含具体方法或函数名
  • -q:静默执行,只展示简略的测试结果
  • -m:执行指定标记的测试用例
  • -k:执行匹配的测试用例,名称包含指定表达式的用例(支持and or not连接)
  • --reruns=${num}:失败重试num次
  • --maxfail=${num}:出现num个用例失败就停止测试
  • -x, --exitfirst:在出现第一个错误或测试失败时立即退出
  • -l, --showlocals:在运行失败后会打印出局部变量名和它们的值,可以省去一些print操作
  • --lf, --last-failed:只重新运行上次失败的用例,如果没有就全都重新执行
  • --ff, --failed-first:运行所有测试,但首先运行上次失败的
  • --tb=${style}:捕捉到失败时输出信息的显示方式
    • --tb=short:仅输出assert的一行及系统判定内容(不显示上下代码)

    • --tb=line:使用一行显示所有错误的信息(最精简)

    • --tb=no:直接屏蔽全部回溯信息

    • --tb=long:输出最详细的回溯信息(最详细)

    • --tb=auto:默认值,如果有多个测试用例失败,仅打印第一个和最后一个的最详细的回溯信息(同long)

    • --tb=native:只输出Python标准库的回溯信息,不显示额外信息

完整参数文档:https://docs.pytest.org/en/latest/reference/reference.html#command-line-flags

7、Exit Code说明

(1)使用场景

在涉及对pytest命令进行二次封装时用到(如建立可视化场景),封装脚本需要对实际运行的pytest命令进行退出码判断,然后进行逻辑判断再返回给实际用户查看。

(2)含义

  • Exit Code 0:所有用例执行完毕,全部通过
  • Exit Code 1:所有用例执行完毕,存在不通过的用例
  • Exit Code 2:用户中断测试执行
  • Exit Code 3:测试执行过程中发生内部错误
  • Exit Code 4:命令行启动错误
  • Exit Code 5:未收集到可执行测试用例

(3)查看退出码

在使用命令行执行pytest xxx.py后,在窗口输入echo $?即可查看到退出码

参考:
http://www.noobyard.com/article/p-wwanxwst-oa.html
https://www.cnblogs.com/sheehan-dali/p/16256212.html

posted @ 2022-08-09 14:42  睡觉大王Risen  阅读(436)  评论(0编辑  收藏  举报