如果你还想从头学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

前言

  • pytest.mark.skip  可以标记无法在某些平台上运行的测试功能,戒者您希望失败的测试功能
  • 希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例
  • 实际常见场景:跳过非Windows平台上的仅Windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试

@pytest.mark.skip

跳过执行测试用例,有可选参数reason:跳过的原因,会在执行结果中打印

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/4/9 13:49
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import pytest @pytest.fixture(autouse=True)
def login():
print("====登录====") def test_case01():
print("我是测试用例11111") @pytest.mark.skip(reason="不执行该用例!!因为没写好!!")
def test_case02():
print("我是测试用例22222") class Test1: def test_1(self):
print("%% 我是类测试用例1111 %%") @pytest.mark.skip(reason="不想执行")
def test_2(self):
print("%% 我是类测试用例2222 %%") @pytest.mark.skip(reason="类也可以跳过不执行")
class TestSkip:
def test_1(self):
print("%% 不会执行 %%")

执行结果

知识点

  • @pytest.mark.skip 可以加在函数上,类上,类方法上
  • 如果加在类上面,类里面的所有测试用例都不会执行
  • 以上小案例都是针对:整个测试用例方法跳过执行,如果想在测试用例执行期间跳过不继续往下执行呢?

pytest.skip()函数基础使用

作用:在测试用例执行期间强制跳过不再执行剩余内容

类似:在Python的循环里面,满足某些条件则break 跳出循环

def test_function():
n = 1
while True:
print(f"这是我第{n}条用例")
n += 1
if n == 5:
pytest.skip("我跑五次了不跑了")

执行结果

pytest.skip(msg="",allow_module_level=False)

当 allow_module_level=True 时,可以设置在模块级别跳过整个模块

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020/4/9 13:49
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import sys
import pytest if sys.platform.startswith("win"):
pytest.skip("skipping windows-only tests", allow_module_level=True) @pytest.fixture(autouse=True)
def login():
print("====登录====") def test_case01():
print("我是测试用例11111")

执行结果

collecting ...
Skipped: skipping windows-only tests
collected 0 items / 1 skipped ============================= 1 skipped in 0.15s ==============================

@pytest.mark.skipif(condition, reason="")

作用:希望有条件地跳过某些测试用例

注意:condition需要返回True才会跳过

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
class TestSkipIf(object):
def test_function(self):
print("不能在window上运行")

执行结果

collecting ... collected 1 item

07skip_sipif.py::TestSkipIf::test_function SKIPPED                       [100%]
Skipped: does not run on windows ============================= 1 skipped in 0.04s ==============================

跳过标记

  • 可以将 pytest.mark.skip 和 pytest.mark.skipif 赋值给一个标记变量
  • 在不同模块之间共享这个标记变量
  • 若有多个模块的测试用例需要用到相同的 skip 或 skipif ,可以用一个单独的文件去管理这些通用标记,然后适用于整个测试用例集
# 标记
skipmark = pytest.mark.skip(reason="不能在window上运行=====")
skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上运行啦啦啦=====") @skipmark
class TestSkip_Mark(object): @skipifmark
def test_function(self):
print("测试标记") def test_def(self):
print("测试标记") @skipmark
def test_skip():
print("测试标记")

执行结果

collecting ... collected 3 items

07skip_sipif.py::TestSkip_Mark::test_function SKIPPED                    [ 33%]
Skipped: 不能在window上运行啦啦啦===== 07skip_sipif.py::TestSkip_Mark::test_def SKIPPED [ 66%]
Skipped: 不能在window上运行===== 07skip_sipif.py::test_skip SKIPPED [100%]
Skipped: 不能在window上运行===== ============================= 3 skipped in 0.04s ==============================

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )

作用:如果缺少某些导入,则跳过模块中的所有测试

参数列表

  • modname:模块名
  • minversion:版本号
  • reasone:跳过原因,默认不给也行
pexpect = pytest.importorskip("pexpect", minversion="0.3")

@pexpect
def test_import():
print("test")

执行结果一:如果找不到module

Skipped: could not import 'pexpect': No module named 'pexpect'
collected 0 items / 1 skipped

执行结果一:如果版本对应不上

Skipped: module 'sys' has __version__ None, required is: '0.3'
collected 0 items / 1 skipped

Pytest系列(7) - skip、skipif跳过用例的更多相关文章

  1. Pytest系列(13)- 重复执行用例插件之pytest-repeat的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 平常在做功能测试的时候,经常 ...

  2. pytest八:skip 跳过用例

    这是一个快速指南,介绍如何在不同情况下跳过模块中的测试1.无条件地跳过模块中的所有测试:pytestmark = pytest.mark.skip("all tests still WIP& ...

  3. pytest测试框架 -- skip跳过执行测试用例

      跳过执行测试用例 1.@pytest.mark.skip(reason=" ") -- 跳过执行测试函数 可传入一个非必须参数reason表示原因 import pytest@ ...

  4. Pytest(9)skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 Skip和xfail: 处理那些不会成功的测试用例 你可以对那些在某些特定平台上不能运行的测试用 ...

  5. 跳过用例skip

    1.装饰器,放在函数前面,跳过用例 @pytest.mark.skip(reason="no way of currently testing this") import pyte ...

  6. 【pytest系列】- pytest测试框架介绍与运行

    如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言​ ​ 目前有两种纯测试的测 ...

  7. pytest(9)-标记用例(指定执行、跳过用例、预期失败)

    pytest中提供的mark模块,可以实现很多功能,如: 标记用例,即打标签 skip.skipif标记跳过,skip跳过当前用例,skipif符合情况则跳过当前用例 xfail标记为预期失败 标记用 ...

  8. Selenium2+python自动化70-unittest之跳过用例(skip)

    前言 当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例 ...

  9. unittest之跳过用例(skip) (含如何调用类里面函数相互调取变量的方法)

    当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例也就没 ...

随机推荐

  1. 【PG】Greenplum-db-6.2.1的安装部署

    目录 1配置host文件(所有节点) 2 配置用户 3 配置/etc/sysctl.conf文件 4 limit文件,后面添加[不影响安装] 5 安装greenplum-db-6.2.1-rhel7- ...

  2. 使用.Net Core编写命令行工具(CLI)

    命令行工具(CLI) 命令行工具(CLI)是在图形用户界面得到普及之前使用最为广泛的用户界面,它通常不支持鼠标,用户通过键盘输入指令,计算机接收到指令后,予以执行. 通常认为,命令行工具(CLI)没有 ...

  3. ggplot2(7) 定位

    7.1 简介 位置调整:调整每个图层中出现重叠的对象的位置,对条形图和其他有组距的图形非常有用: 位置标度:控制数据到图形中位置的映射,常用的是对数变换: 分面:先将数据集划分为多个子集,然后将每个子 ...

  4. URL及short URL短网址

    URL,uniform resource locator,经常被称为网址,尤其是在使用HTTP的时候.通常是一个指向某个资源的字符串.   URLs经常被用于网页(http),但也可以用于文件传输(f ...

  5. ipadmini从9.3.5降级8.4.1并完美越狱

    ipadmini之前是iOS9.3.5实在是卡的用不了,于是打算降级,但是尝试了包括改版本描述等很多方法一直失败.今天突然成功降级8.4.1并且完美越狱,运行流畅了非常多.赶紧发个教程,回馈一下网友. ...

  6. java第一次上机练习作业

    1.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序.(知识点:变量和 运算符综合应用) int a = 5, b = 10; int temp; temp = a; a = b; b = ...

  7. 【Weiss】【第03章】练习3.7:有序多项式相乘

    [练习3.7] 编写一个函数将两个多项式相乘,用一个链表实现.你必须保证输出的多项式按幂次排列,并且任意幂次最多只有一项. a.给出以O(M2N2)时间求解该问题的算法. b.写一个以O(M2N)时间 ...

  8. 基于 HTML5 WebGL 与 GIS 的智慧机场大数据可视化分析

    前言:大数据,人工智能,工业物联网,5G 已经或者正在潜移默化地改变着我们的生活.在信息技术快速发展的时代,谁能抓住数据的核心,利用有效的方法对数据做数据挖掘和数据分析,从数据中发现趋势,谁就能做到精 ...

  9. Mol Cell Proteomics. | Identification of salivary biomarkers for oral cancer detection with untargeted and targeted quantitative proteomics approaches (解读人:卜繁宇)

    文献名:Identification of salivary biomarkers for oral cancer detection with untargeted and targeted qua ...

  10. C++技法杂记

    C++ 技法杂技杂记 1. 枚举 1.1 枚举继承(Enum Inheritance) struct Enum{ enum{ One = 1, Two, Last }; }; struct EnumD ...