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

前置条件:

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

  

原文地址:https://www.cnblogs.com/wxcx/p/13795743.html