目录

一、简介

二、使用方法

1、@pytest.mark.skip装饰器

2、pytest.skip方法

3、@pytest.mark.skipif装饰器

4、pytestmark变量

5、conftest.py

三、执行方法


一、简介

最近在写自动化用例时遇到了一个场景,某些用例是只针对CentOS操作系统的,其它系统需要跳过它们。这种情况下,可以使用skip、skipif等实现跳过测试用例的功能。

二、使用方法

1、@pytest.mark.skip装饰器

在用例或类前面加上@pytest.mark.skip装饰器,并传入原因,可以跳过用例或类。参数如下:

pytest.mark.skip(*, reason=None)

※  reason (str) – Reason why the test function is being skipped.

@pytest.mark.skip(reason="This feature has not yet been implemented")
def test_aaa(): ……

2、pytest.skip方法

在测试执行过程中调用pytest.skip(reason)方法,使用给定的消息跳过正在执行的用例:

skip(msg[, allow_module_level=False])

※  allow_module_level (bool) – Allows this function to be called at module level, skipping the rest of the module. Defaults to False.

def test_function():if not valid_config():pytest.skip("unsupported configuration")

当allow_moudel_level=True时,将跳过整个模块:

if not pytest.config.getoption("--custom-flag"):pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)

3、@pytest.mark.skipif装饰器

在用例或类前面加上@pytest.mark.skipif装饰器,可以有条件的跳过用例或类:

pytest.mark.skipif(condition, *, reason=None)

※  condition (bool or str) – True/False if the condition should be skipped or a condition string.

※  reason (str) – Reason why the test function is being skipped.

@pytest.mark.skipif(sys.version_info < (3.6), reason="requires python3.6 or higher")
def test_bbb():...

因为conftest.py是在@pytest.mark.skipif装饰器执行之后才加载的,所以如果想通过判断命令行选项的值来决定是否跳过某条用例的话,pytest.skip方法显然是更好的选择

4、pytestmark变量

定义pytestmark变量,这个变量将作用于整个模块:

import pytestpytestmark = pytest.mark.skip('作用于模块中的每一个用例,所以test_ccc、test_ddd都将跳过')def test_ccc():assert Truedef test_ddd():assert True

运行结果:

>>>pytest test.py
========================================================== test session starts =============================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: D:\6-0-Prepare_Job\TestCase
plugins: allure-pytest-2.8.18, html-2.1.1, metadata-1.10.0
collected 2 items                                                                                                                           test.py ss                                                                                                                            [100%]========================================================== 2 skipped in 0.02s =============================================================

5、conftest.py

在conftest.py中配置collect_ignore_glob项, 可以在用例的收集阶段跳过指定的文件和目录。例如,跳过当前测试目录中文件名匹配test_*.py规则的文件和eee的子文件夹fff中的文件:

collect_ignore_glob = ['test*.py', 'eee/fff']

三、执行方法

pytest 默认不显示skip和xfail用例的详细信息,我们可以通过-r选项使其显示出来。通常一个字母代表一种类型,具体的规则为:

(f)ailed, (E)rror, (s)kipped, (x)failed, (X)passed, (p)assed, (P)assed with output, (a)ll except passed(p/P), or (A)ll

所以,显示结果为SKIPPED的用例:

pytest -rs

pytest测试框架(六)---使用skip和skipif跳过测试用例相关推荐

  1. Pytest测试框架(五):pytest + allure生成测试报告

    系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...

  2. Pytest测试框架(二):pytest 的setup/teardown方法

    系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...

  3. Python编程必不可少的pytest测试框架

    进行编程测试重要的是为了更高效的完成功能的实现. pytest是基于unittest实现的第三方测试框架,比 unittest 更加的简洁.高效,并且可以完美兼容 unittest 的测试代码,无需对 ...

  4. Pytest 测试框架——数据驱动

    引言 前面已经和大家介绍过 Unittest 测试框架的数据驱动框架 DDT,以及其实现原理.今天和大家分享的是 Pytest 测试框架的数据驱动,Pytest 测试框架的数据驱动是由 pytest ...

  5. pytest测试框架_聊聊 Python 的单元测试框架(三):最火的 pytest

    本文首发于 HelloGitHub 公众号,并发表于 Prodesire 博客. 一.介绍 本篇文章是<聊聊 Python 的单元测试框架>的第三篇,前两篇分别介绍了标准库 unittes ...

  6. pytest测试框架(一):初识pytest

    一.pytest的简介 pytest是一个非常成熟的python的单元测试框架,比unittest更灵活,容易上手. pytest可以和selenium.request,appium结合实现web自动 ...

  7. pytest测试框架详细讲解part01

    文章目录 一.单元测试框架 1.什么是单元测试框架 2.单元测试框架分类 3.单元测试框架主要做什么? 二.Pytest简介以及常用插件安装 三.pytest默认测试用例的规则以及基础应用 四.pyt ...

  8. Web自动化之Pytest测试框架

    pytest是一个python的单元测试框架,可以用于自动化测试中. 用例规则 pytest命令执行测试时,如果我们不指定具体的文件,PyTest默认从当前路径及其所有子目录中搜索py源文件,所有名字 ...

  9. pytest测试框架——allure报告

    文章目录 一.allure的介绍 二.allure的运行方式 三.allure报告的生成 方式一.在线报告.会直接打开默认浏览器展示当前报告 方式二.静态资源文件报告(带index.html.css. ...

最新文章

  1. OpenGL编程轻松入门(四)
  2. 全检体系结构风格浅谈
  3. 人生哲理名言六十六条
  4. Spring 进阶二
  5. 逆向最大匹配分词算法
  6. 不同层次程序员的比较:三流比设计,一流比方法,顶级比什么?
  7. oracle+生成+sql语句,Oracle使用SQL语句生成日历的实现方法
  8. php 登陆微博,用新浪微博账号登录(第三方登录)
  9. ETL数据处理平台,快速实现数据集成
  10. _initialize() 区别 __construct()
  11. 弘辽科技:网店点击率低怎么办?点击率多少正常?
  12. 华硕笔记本能通用的BIOS型号
  13. 【术语】本地部署、云化部署、混合部署
  14. matlab 524288,Cannot display summaries of variables with more than 524288 elements. 怎么...
  15. 连肝7个晚上,总结了计算机网络的知识点!(共66条)
  16. DNA序列 UVa1368
  17. 与设备无关的彩色空间
  18. 求解全微分的原函数(二元)
  19. 光伏逆变器“核心器件”IGBT在光伏逆变器的应用
  20. 猜数字游戏 - Java实现

热门文章

  1. python pillow库下载_054-python库Pillow
  2. 人才梯队的搭建:55页集团人才梯队建设方案,梯队人员的管理
  3. 解决重启VCSA 6.0,访问vsphere web client提示:503 Service Unavailable错误
  4. VSAN 个版本对应vSphere版本
  5. 虚拟化小白对VMcpu分配的理解
  6. Docker学习总结(8)——利用Docker开启持续交付之路
  7. hystrix 页面_《SpringCloud微服务之Hystrix组件》
  8. Current在Java里面_在C#中相当于Java System.currentTimeMillis()
  9. mysql不能访问order,使用ORDER BY时,MySQL不使用索引(“Using filesort”)
  10. java登录验证技术,login_required -- 登录验证