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 等),以便根据需要执行或排除特定类型的测试。
  • 为测试用例添加额外的元数据,以便在运行时执行特定操作或生成详细的测试报告。
  • 使用标记来控制测试的执行顺序或并发度。
  • 通过自定义标记实现参数化测试,灵活地处理不同的测试场景。
  • 确保关键测试优先执行:你可以为关键测试添加高优先级标记,以确保它们在其他测试之前运行,从而及早发现潜在问题。
  • 控制测试顺序:如果你需要控制测试的执行顺序,可以使用优先级标记来定义测试的执行顺序,以满足特定需求。
相关推荐
小鸿的摸鱼日常15 分钟前
Kivy,一个上天入地的 Python 库
python·kivy·python 库之旅
Banana084040 分钟前
seq2seq翻译实战-Pytorch复现
人工智能·pytorch·python
计算机编程-吉哥42 分钟前
计算机毕业设计 基于Python的汽车销售管理系统 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·计算机毕业设计·计算机毕业论文·计算机毕业设计选题·软件工程毕业设计论文·汽车销售管理系统
mariokkm2 小时前
Python一分钟:装饰器
开发语言·python
不良人龍木木2 小时前
环境部署-环境变量
linux·python
eeee~~2 小时前
特征工程与交叉验证在机器学习中的应用
python·机器学习·jupyter·信息可视化·数据分析
好度2 小时前
python网站创建001:内容概览
开发语言·python
手可摘云朵2 小时前
8.sklearn-模型保存
人工智能·python·sklearn
我的心永远是冰冰哒2 小时前
服务器安装pytorch_geometric torch_scatter踩坑记录
服务器·pytorch·python
姚杰献2 小时前
MacOS上安装MiniConda的详细步骤
人工智能·python·深度学习·macos·机器学习·conda·mac