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

目录

一、简介

二、使用方法

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 pytest

pytestmark = pytest.mark.skip('作用于模块中的每一个用例,所以test_ccc、test_ddd都将跳过')

def test_ccc():
    assert True

def 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

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:在pytest中,可以使用`pytest.skip`函数来跳过测试用例。可以在测试函数或测试类中使用`pytest.skip`函数来指定跳过的原因。例如,在测试函数中使用`pytest.skip`函数可以跳过测试函数,并在跳过时提供一个消息。示例代码如下: ```python import pytest def test_01(): print("我是测试函数。") pytest.skip(msg="跳过此条用例。") assert 1 == 1 class TestSkip: def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): pytest.skip(msg="跳过此条用例。") print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,`test_01`函数和`test_two`方法都使用了`pytest.skip`函数来跳过测试用例,并提供了跳过的原因。当运行这些测试用例时,它们将被跳过执行。 引用\[2\]:另一种在pytest跳过测试用例的方法是使用`pytest.mark.skip`装饰器。可以在测试函数或测试类上使用`pytest.mark.skip`装饰器来指定跳过的原因。示例代码如下: ```python import pytest pytestmark = pytest.mark.skip(reason="跳过当前模块") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,使用`pytest.mark.skip`装饰器标记了整个模块为跳过状态,并提供了跳过的原因。当运行这个模块的测试用例时,所有的测试用例都将被跳过执行。 引用\[3\]:还可以使用`pytest.mark.skip`装饰器来跳过测试函数或测试类。示例代码如下: ```python import pytest @pytest.mark.skip(reason="跳过测试函数的测试case") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: @pytest.mark.skip(reason="跳过类里面case") def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,使用`pytest.mark.skip`装饰器标记了`test_01`函数和`test_one`方法为跳过状态,并提供了跳过的原因。当运行这些测试用例时,它们将被跳过执行。 综上所述,pytest中可以使用`pytest.skip`函数或`pytest.mark.skip`装饰器来跳过测试用例,并提供跳过的原因。 #### 引用[.reference_title] - *1* *2* *3* [pytest测试框架系列 - Pytest skipskipif 跳过用例看这篇就够了!](https://blog.csdn.net/u010454117/article/details/118469127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值