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

pytest框架格式+setup 函数和 teardown 函数和setup_class 和 teardown_class 函数

武飞扬头像
枯泔U
帮助2

安装 pytest 库  pytest 5.3.5
1. 符合 pytest 框架结构的项目目录结构 2. 测试脚本的文件命名规则 :test_*.py
3. 测试类的命名规则: Test*
4. 测试函数的命名规则 :test_*_001
5. 测试函数的三大组成部分
部分 1 :接收测试数据
部分 2 :写 UI 上业务步骤 / http 请求
部分 3 :断言
6. 测试函数断言逻辑
assert xxx // 如果 xxx True ,测试通过;是 False ,测试不通过
7. 如何运行单个测试脚本
DOS>pytest -s 脚本路径
8. 有了 pytest 配置文件,如何批量运行测试脚本
DOS>pytest
pytest.ini:// 项目根目录中。 GBK 编码!
[pytest]
# 添加命令行参数
addopts = -s
# 搜索哪个文件夹
testpaths = ./script
# 文件名 python_files = test_*.py
# 类名
python_classes = Test*
# 函数名
python_functions = test_*
9. 1 UI 对应 N 个测试脚本。 1 个测试脚本对应 1 个测试类。一个测试类对应 N 个测试函
数。一个测试函数对应 N 个相同 / 相似断言逻辑的测试用例!
=====================================================================
setup 函数:用于做初始化工作!特点:测试类中每个测试函数执行 1 次, setup 函数也会执
1 次! 即测试类中有 N 个测试函数,那么 setup 函数就会执行 N 次!
teardown 函数:用于做扫尾工作!特殊:测试类中每个测试函数执行 1 次, teardown 函数
也会执行 1 次! 即测试类中有 N 个测试函数,那么 teardown 函数就会执行 N 次!
假设:
某测试类中有 2 个测试函数 test_01 test_02 setup teardown 4 个函数!
结论:
setup->test01->teardown
setup->test02>teardown
=========================================================================
这两个函数属于类级别, 一个测试类中不管有几个测试函数,这两个函数都只会运行一次
setup_class: 用于类的初始化工作,比如启动 app
Teardown_class: 用于类的销毁功能,比如清理残留!
如果一个测试类中有 setup_class setup teardown teardown_class 、两个测试函数,那么
执行顺序是这样的:
setup_class setup
test_01
teardown
setup
test_02
teardown
teardown_class

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

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