Pytest教程:Pytest的跳过与标记功能用法

一、pytest跳过功能


1. 使用 @pytest.skip 装饰器跳过测试用例

Pytest中的 @pytest.skip 装饰器允许你跳过指定的测试用例。你可以将该装饰器应用于测试函数或测试类上。

python 复制代码
import pytest

@pytest.mark.skip(reason="Skipping this test case")
def test_skip():
    assert 1 + 1 == 3

@pytest.mark.skipif(sys.version_info < (3, 7), reason="Python version is below 3.7")
def test_skip_if():
    assert 2 * 3 == 6

@pytest.mark.skip("Outdated feature, skipping test")
def test_skip_with_reason():
    assert "hello".upper() == "HELLO"

在上面的示例中,我们使用了不同的方式来标记需要跳过的测试用例。@pytest.skip 装饰器可以接受一个可选的 reason 参数,用于提供跳过测试的原因。

2. 使用 pytest.skip() 函数动态跳过测试用例

除了使用装饰器跳过测试用例外,你还可以在测试函数中使用 pytest.skip() 函数来动态地跳过测试。

python 复制代码
import pytest

def test_skip_dynamic():
    condition = False  # 设置一个条件,决定是否跳过测试用例
    if condition:
        pytest.skip("Skipping this test dynamically")
    assert 10 / 2 == 5

在上述示例中,我们根据条件动态地决定是否跳过测试用例。如果条件为真,那么测试用例将被跳过,并在测试报告中显示跳过的原因。

3. 使用 pytest.mark.skippytest.mark.skipif 跳过测试函数或模块

除了跳过单个测试用例外,你还可以使用 pytest.mark.skippytest.mark.skipif 标记整个测试函数或测试模块。

python 复制代码
import pytest

@pytest.mark.skip(reason="Skipping the entire test function")
def test_skip_function():
    assert 1 + 1 == 3

@pytest.mark.skipif(sys.platform != "win32", reason="Only runs on Windows platform")
def test_skip_module():
    assert "hello" != "world"

在上面的示例中,我们使用 @pytest.mark.skip 装饰器标记了整个测试函数,以及使用 @pytest.mark.skipif 装饰器标记了整个测试模块。这样,这些被标记的测试将在运行时被跳过。

4. 应用场景

跳过功能在以下常见的应用场景中非常有用:

  • 当某些条件不满足时,可以跳过相关的测试用例。例如,跳过需要特定环境或特定版本的测试。
  • 当某些功能被废弃或不再可用时,可以跳过相关的测试用例,以避免浪费时间和资源。
  • 在进行持续集成(CI)时,可以根据特定的条件跳过一些测试用例,以加快构建和部署过程。

二、pytest标记功能


1. 使用 @pytest.mark 装饰器添加标记

你可以使用 @pytest.mark 装饰器为测试函数、类或模块添加标记。这样可以将不同的标记应用于不同的测试,以便在运行时根据标记执行相应的操作。

python 复制代码
import pytest

@pytest.mark.smoke
def test_login():
    assert True

@pytest.mark.regression
class TestUser:
    def test_create_user(self):
        assert True

在上述示例中,我们使用 @pytest.mark 装饰器分别为一个测试函数和一个测试类添加了不同的标记(smokeregression)。这样就可以根据标记来执行特定类型的测试。

2. 运行带有特定标记的测试

你可以使用 -m 参数来运行带有特定标记的测试。例如,要只运行带有 smoke 标记的测试,可以运行以下命令:

pytest -m smoke

这将只运行带有 smoke 标记的测试用例。

3. 自定义标记参数化

除了预定义的标记外,你还可以定义自己的标记,并将其用于参数化测试。这样可以为不同的测试场景提供更多灵活性。

python 复制代码
import pytest

@pytest.mark.parametrize("input, expected", [
    (1, 2),
    (2, 4),
    (3, 6)
], ids=["input_1", "input_2", "input_3"])
def test_multiply(input, expected):
    assert input * 2 == expected

在上述示例中,我们定义了一个带有参数化标记的测试函数。这样可以为不同的输入数据提供不同的期望输出,并在报告中使用自定义的标识符(ids)来标记每组参数化测试。

4.标记优先级

Pytest 中的标记优先级是通过 @pytest.mark 装饰器的 priority 参数实现的。你可以为测试函数、类或模块添加优先级标记,以控制它们的执行顺序。

python 复制代码
import pytest

@pytest.mark.smoke
@pytest.mark.priority(1)
def test_login():
    assert True

@pytest.mark.regression
@pytest.mark.priority(2)
class TestUser:
    def test_create_user(self):
        assert True

在上述示例中,我们通过 @pytest.mark.priority 装饰器为测试函数和测试类分别设置了不同的优先级。这样就可以确保在运行测试时,具有更高优先级的测试将首先执行。

当你运行带有优先级标记的测试时,Pytest 将按照优先级顺序执行测试用例。例如,要运行带有优先级标记的测试,可以运行以下命令:

pytest --prioritize

5. 标记应用场景

标记功能在以下情况下特别有用:

  • 将测试用例分组为不同的类别(如 smoke、regression、performance 等),以便根据需要执行或排除特定类型的测试。
  • 为测试用例添加额外的元数据,以便在运行时执行特定操作或生成详细的测试报告。
  • 使用标记来控制测试的执行顺序或并发度。
  • 通过自定义标记实现参数化测试,灵活地处理不同的测试场景。
  • 确保关键测试优先执行:你可以为关键测试添加高优先级标记,以确保它们在其他测试之前运行,从而及早发现潜在问题。
  • 控制测试顺序:如果你需要控制测试的执行顺序,可以使用优先级标记来定义测试的执行顺序,以满足特定需求。
相关推荐
万添裁11 分钟前
pytorch的张量数据结构以及各种操作函数的底层原理
人工智能·pytorch·python
浔川python社22 分钟前
张雪机车:以热爱为轮,让中国摩托驰骋世界之巅
python
zl_dfq28 分钟前
Python学习5 之【字符串】
python·学习
ZC跨境爬虫38 分钟前
Python异步IO详解:原理、应用场景与实战指南(高并发爬虫首选)
爬虫·python·算法·自动化
倦王1 小时前
力扣日刷47-补
python·算法·leetcode
2501_921649491 小时前
原油期货量化策略开发:历史 K 线获取、RSI、MACD 布林带计算到多指标共振策略回测
后端·python·金融·数据分析·restful
真心喜欢你吖1 小时前
统信操作系统UOS部署安装OpenClaw+飞书接入完整教程(国产大模型配置)
人工智能·python·语言模型·大模型·openclaw·小龙虾
用户8356290780511 小时前
使用 Python 自动生成 Excel 柱状图的完整指南
后端·python
xcbrand1 小时前
口碑好的品牌策划厂家
大数据·人工智能·python