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 等),以便根据需要执行或排除特定类型的测试。
  • 为测试用例添加额外的元数据,以便在运行时执行特定操作或生成详细的测试报告。
  • 使用标记来控制测试的执行顺序或并发度。
  • 通过自定义标记实现参数化测试,灵活地处理不同的测试场景。
  • 确保关键测试优先执行:你可以为关键测试添加高优先级标记,以确保它们在其他测试之前运行,从而及早发现潜在问题。
  • 控制测试顺序:如果你需要控制测试的执行顺序,可以使用优先级标记来定义测试的执行顺序,以满足特定需求。
相关推荐
悠悠小茉莉31 分钟前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
m0_625686551 小时前
day53
python
Real_man1 小时前
告别 requirements.txt,拥抱 pyproject.toml和uv的现代Python工作流
python
站大爷IP2 小时前
Python文件操作的"保险箱":with语句深度实战指南
python
运器1232 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
巴里巴气4 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19894 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金5 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen5 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm
前端付豪5 小时前
20、用 Python + API 打造终端天气预报工具(支持城市查询、天气图标、美化输出🧊
后端·python