Python测试框架之pytest高阶用法之跳过(Skip)及预期失败(xFail): 处理不能成功的测试用例(四)-晒酷学院

晒酷学院:https://shareku.ke.qq.com/
微信号:添加请注明晒酷学院            QQ群:979438600
微信号:添加请注明晒酷学院         QQ群:979438600
       
前置条件:

1.文件路径:

- Test_App
- - test_abc.py
- - pytest.ini

2.pyetst.ini配置文件内容:

[pytest]
命令行参数
addopts = -s
搜索文件名
python_files = test_*.py
 搜索的类名
python_classes = Test_*
 搜索的函数名
python_functions = test_*

3.1 跳过测试函数

根据特定的条件,不执行标识的测试函数.
 方法:
     skipif(condition, reason=None)
 参数:
     condition:跳过的条件,必传参数
     reason:标注原因,必传参数
 使用方法:
     @pytest.mark.skipif(condition, reason="xxx")
import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.skipif(condition=2 > 1, reason="跳过该函数")  # 跳过测试函数test_b
    def test_b(self):
        print("------->test_b")
        assert 0

在这里插入图片描述
3.2 @pytest.mark.skip(reason=" ") – 跳过执行测试函数
可传入一个非必须参数reason表示原因

import pytest
@pytest.mark.skip(reason="no reason")
def test_01():
  print("---用例a执行---")
class Test_Case():
  @pytest.mark.skip(reason="no reason")
  def test_02(self):
    print("---用例b执行---")
 
  def test_03(self):
    print("---用例c执行---")

在这里插入图片描述
3.3 自定义@pytest.mark.skip()标签
myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=…)
装饰时用该变量代替标签即可:@myskip

import pytest
# myskip = pytest.mark.skip()
myskip = pytest.mark.skipif(condition=2>1, reason="no reason")
@myskip
def test_01():
  print("---用例a执行---")
class Test_Case():
  @myskip
  def test_02(self):
    print("---用例b执行---")
  def test_03(self):
    print("---用例c执行---")

在这里插入图片描述
3.4 通过pytest.skip()方法跳过测试函数

import pytest
def test_01():
  pytest.skip(msg="no reason")
  print("---用例a执行---")
class Test_Case():
  def test_02(self):
    pytest.skip()
    print("---用例b执行---")
  def test_03(self):
    print("---用例c执行---")

在这里插入图片描述
3.5 跳过测试类
跳过测试类其实和跳过测试方法一样,使用@pytest.mark.skip()和@pytest.mark.skipif()两个标签,用他们装饰测试类就好啦。
根据某些条件跳过模块中的所有测试用例如:
pytestmark = pytest.mark.skipif(sys.platform == “win32”,reason=“tests for linux only”)

import pytest
myskip = pytest.mark.skip(reason="no reason")
def test_01():
  print("---用例a执行---")
@myskip
class Test_Case():
  def test_02(self):
    print("---用例b执行---")
  def test_03(self):
    print("---用例c执行---") 

在这里插入图片描述
3.6 跳过模块
使用pytestmark(不可更改变量名)变量,让他等于标签即可。

import pytest
#pytestmark = pytest.mark.skip()
pytestmark = pytest.mark.skip(reason='no reason')
def test_01():
  print("---用例a执行---")
class Test_Case():
  def test_02(self):
    print("---用例b执行---")
  def test_03(self):
    print("---用例c执行---")

在这里插入图片描述
** 4.1 标记为预期失败函数**

标记测试函数为失败函数
 方法:
     xfail(condition=None, reason=None, raises=None, run=True, strict=False)
 常用参数:
     condition:预期失败的条件,必传参数
     reason:失败的原因,必传参数
 使用方法:
     @pytest.mark.xfail(condition, reason="xx")
import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail(2 > 1, reason="标注为预期失败") # 标记为预期失败函数test_b
    def test_b(self):
        print("------->test_b")
        assert 0

x # 失败标记
在这里插入图片描述
4.2使用xfail标记指示你希望测试失败:

@pytest.mark.xfail
def test_function():
    ...
import pytest
class Test_ABC:
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail
    def test_b(self):
        print("------->test_b")
        assert 0

将运行此测试,但在失败时不会报告回溯。相反,终端报告会将其列在“预期失败”(XFAIL)或“意外传递”(XPASS)部分中。

或者,也可以xfail在测试用例或setup函数中强制标记测试预期失败:

def test_function():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)")
import pytest
class Test_ABC:
    def test_a(self):
        print("------->test_a")
        assert 1
    def test_b(self):
        if 2>1:
            pytest.xfail("failing configuration (but should work)")
        print("------->test_b")

在这里插入图片描述
这将无条件地制作test_function``XFAIL。请注意,pytest.xfail调用后不会执行其他代码,与标记不同。那是因为它是通过引发已知异常在内部实​​现的。

4.3 strict参数(默认是false)
如果strict参数设置为True, 如果出现xpass,测试套件的结果将视为失败。:

@pytest.mark.xfail(strict=True)
def test_function():
    ...
import pytest
class Test_ABC:
    def test_a(self,b=1):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail(strict=True)
    def test_b(self):
        print("------->test_b")
        assert 1
  

在这里插入图片描述
这将导致`xpass(“意外通过”)此测试的结果导致测试套件失败。

strict参数也可以使用xfail_strict变量配置到pytest.ini文件中。:

[pytest]
xfail_strict=true

4.4 reason参数
与skipif一样,你也可以在特定平台上标记你对失败的期望:

@pytest.mark.xfail(sys.version_info >= (3,6),reason="Python3.6 API变更")
def test_function():
    ...  

4.5 raises参数
如果你想更具体地说明测试失败的原因,可以在raises参数中指定单个异常或异常元组。

@pytest.mark.xfail(raises=RuntimeError)
def test_function():
    ...

4.6 忽略xfail
通过在命令行上指定:

pytest --runxfail

可以强制运行并报告xfail标记的测试,就像它根本没有标记一样。这也导致pytest.xfail没有效果。

import pytest
class Test_ABC:
    def test_a(self,b=1):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail
    def test_b(self):
        print("------->test_b")
        assert 1

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值