自动化测试执行过程中,我们常常出现这种情况:因为功能阻塞,未实现或者环境有问题等等原因,一些用例执行不了, 如果我们注释掉或删除掉这些测试用例,后面可能还要进行恢复操作,这时我们就可以配置跳过这些用例。

Pytest测试框架中存在两个跳过测试的方法:skipskipif

1、无条件跳过skip

skip方法为无条件跳过测试用例。

使用方法:@pytest.mark.skip标记在需要跳过的测试用例上。

2、有条件跳过skipif

skipif方法为有条件跳过测试用例,条件为真跳过。

使用方法:@pytest.mark.skipif(condition=跳过的条件, reason=跳过的原因),

标记在需要符合条件跳过的测试用例上。

  • 参数condition:跳过的条件,为True则跳过测试,为False则继续执行测试,默认为True。
  • 参数reason:标注跳过的原因,必填参数。

3、练习

"""
1.学习目标
掌握pytest中跳过测试方法
2.操作步骤
skipif(condition=判断条件,reason=跳过原因)
使用时放置在需要跳过的用例之前
@pytest.mark.skipif(条件,原因) # 当条件为真,跳过执行
3.需求
"""
# 导入pytest
import pytest # 编写测试用例
def login_data():
return "jerry", "123456" # 无条件跳过
@pytest.mark.skip
def test_register():
"""注册用例"""
print("注册步骤")
assert False # 当条件为真,跳过测试
@pytest.mark.skipif(login_data()[0] == "jerry", reason="jerry用户不存在")
def test_login():
"""不记住密码登录"""
username = login_data()[0]
password = login_data()[1]
print(f"输入用户名{username}")
print(f"输入密码{password}")
print("点击登录按钮")
assert username == "jerry" def test_shopping():
"""购物下单"""
print("购物流程")
assert True if __name__ == '__main__':
pytest.main() """
执行结果:跳过一个用例 : 1通过,2跳过 test_pytest_01.py::test_register
test_pytest_01.py::test_login
test_pytest_01.py::test_shopping ======================== 1 passed, 2 skipped in 0.04s ========================= Process finished with exit code 0
SKIPPED (unconditional skip)
Skipped: unconditional skip
SKIPPED (jerry用户不存在)
Skipped: jerry用户不存在
购物流程
PASSED
"""
# 注:跳过的用例测试结果标识为s

最新文章

  1. eclipse 提示错误**cannot be resolved to a type
  2. codevs 1227 方格取数 2
  3. tcp 重发 应用层重传
  4. HTTP权威指南一
  5. 修改一行代码提升 Postgres 性能 100 倍
  6. How to learn C++ and find all STL Algorithm reference
  7. NET基础课--XML基础
  8. 64位系统访问注册表SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  9. 第33篇 js 常用简单的写法
  10. Java项目案例:酒店前台客服管理系统
  11. 洛谷 p2440木材加工
  12. Python 练习:三级菜单选择城市(二)
  13. Python学习1 基础数据类型
  14. 解题:CF983A Finite or not
  15. 原生javascript知识点
  16. elasticsearch中的mapping简介
  17. 大数据技术原理与应用——大数据处理架构Hadoop
  18. 初识.NET Core
  19. 【转】IOS基础:深入理解Objective-c中@class的含义
  20. $'\r': command not found 或者 syntax error: unexpected end of file 或者 syntax error near unexpected token `$'\r''

热门文章

  1. Solon 开发,一、注入或手动获取配置
  2. 《剑指offer》面试题18. 删除链表的节点
  3. 【记录一个问题】android下的ucontext协程,因为使用栈上的对象,导致cv::Mat被莫名析构
  4. Markdown最新使用说明
  5. Go 函数,包(二)
  6. 推荐一个github国内访问加速神器GitHub520
  7. 微服务探索之路03篇-docker私有仓库Harbor搭建+Kubernetes(k8s)部署私有仓库的镜像
  8. WinForms 获取文件夹的基本信息
  9. Network Kit与三七游戏共创流畅游戏体验,无惧网络延迟
  10. NSInvocation的基本使用