有没有可能按照这样的方式安排测试,即测试A将首先进行,然后测试B第二次,然后测试A将再次作为第三? 我用的是pytest-order磅.

技术堆栈:100101102

以下是使用的代码:

import logging
import pytest


@pytest.mark.order(2)
def test_a():
    logging.info('test_a')
    pass


@pytest.mark.order(1)
@pytest.mark.order(3)
def test_b():
    logging.info('test_b')
    pass

但测试B不会第三次执行.只有一次因为两个order马克而成为第一个.

Output:

=

收集...已收集2件物品

Test.py::test_a通过[50%]

通过了[100%]的测试.

=

推荐答案

实际上,pytest-order不允许将两个order相加.然而,我为你想出了一些解决方案.

您可以使用pytest_generate_tests最小钩子来解析此语法.只需将其添加到您的conftest.py文件中.

下面的示例将读取所有pytest.mark.order个标记并对其进行参数化测试(如果提供了一个以上的订单标记).它添加了名为order的参数,该参数存储在pytest.mark.order中指定的参数.

conftest.py

def _get_mark_description(mark):
    if mark.kwargs:
        return ", ".join([f"{k}={v}" for k, v in mark.kwargs.items()])
    elif mark.args:
        return f"index={mark.args[0]}"
    return mark


def pytest_generate_tests(metafunc):
    """
    Handle multiple pytest.mark.order decorators.

    Make parametrized tests with corresponding order marks.
    """
    if getattr(metafunc, "function", False):
        if getattr(metafunc.function, "pytestmark", False):
            # Get list of order marks
            marks = metafunc.function.pytestmark
            order_marks = [
                mark for mark in marks if mark.name == "order"
            ]
            if len(order_marks) > 1:
                # Remove all order marks
                metafunc.function.pytestmark = [
                    mark for mark in marks if mark.name != "order"
                ]
                # Prepare arguments for parametrization with order marks
                args = [
                    pytest.param(_get_mark_description(mark), marks=[mark])
                    for mark in order_marks
                ]
                if "order" not in metafunc.fixturenames:
                    metafunc.fixturenames.append("order")
                metafunc.parametrize('order', args)

test_order.py

import pytest


@pytest.mark.order(6)
@pytest.mark.order(4)
def test_4_and_6():
    pass


@pytest.mark.order(5)
@pytest.mark.order(3)
@pytest.mark.order(1)
def test_1_3_and_5():
    pass


@pytest.mark.order(2)
def test_2():
    pass

100 Output

collecting ... collected 6 items

test_order.py::test_1_3_and_5[index=1] 
test_order.py::test_2 
test_order.py::test_1_3_and_5[index=3] 
test_order.py::test_4_and_6[index=4] 
test_order.py::test_1_3_and_5[index=5] 
test_order.py::test_4_and_6[index=6] 

如您所见,所有测试都以定义的顺序运行.

UPD

我已经更新了钩子,使其与pytest-order的其他功能兼容.我也创造了PR in pytest-order GitHub repo个.

Python-3.x相关问答推荐

循环遍历数据框以提取特定值

Python多处理池:缺少一个进程

如何在matplotlib中显示次要刻度标签

Django将任何查询显示为html表格

我们可以在每个可以使用 Pandas Join 的用例中使用 Pandas merge 吗?

CDKTF ec2 具有特定私有 IP 地址的娱乐

如何将日期时间索引写入日期类型的表?

在新数据帧上自动提取两个字符串 python 之间的相等性

attrs 将 list[str] 转换为 list[float]

通过附加/包含多个列表来创建 nDimensional 列表

spaCy 中的匹配模式返回空结果

Semaphore信号量 Python 的工作原理

无法在 Windows Python 3.5 上安装 Levenshtein 距离包

Python:遍历子列表

在 Pandas 数据框中显示对图

Pandas 的 EMA 与股票的 EMA 不匹配?

如何调试垂死的 Jupyter Python3 内核?

无法在 Windows 8 中使用 Python 3.3 找到 vcvarsall.bat

如何为 anaconda python3 安装 gi 模块?

如何从集合中删除多个元素?