python - 如何在 pytest 中使用具有不同数据结构的不同 fixture 进行一次测试?

标签 python pytest

我有一个测试,目前使用的是冒烟测试装置,它覆盖了完整测试装置的一个子集。对于测试的一部分,我想使用冒烟测试 fixture 进行测试,但是如果我想进行大型测试,我想使用采用不同数据结构的完整测试 fixture (在下面的例子,small 使用元组,big 使用列表)。目前,我使用如下所示的一个 fixture 进行测试,但我无法弄清楚如何在具有不同数据结构的 fixture 之间进行互换。

import pytest

@pytest.fixture(params=[
    (1, 1),
    (2, 2)
])
def small_fixture_of_numbers(request):
    # returns a list of pairs of numbers for smoke testing
    return request.param

@pytest.fixture(params=[
    1, 2, 3, 4
])
def big_fixture_of_numbers(request):
    # returns the full list of numbers for full-scale testing
    return request.param

def test_addition(small_fixture_of_numbers):
    (x, y) = small_fixture_of_numbers
    total = x + y
    assert total > 2

最佳答案

以不同方式运行相同的测试是不明智的。如果不同的运行由不同的数据结构支持,则更不用说。

测试用例的整体理念是,它为代码每次都在相同条件下运行提供了一个稳定的环境。测试和 fixture 应该是固定的伙伴,因此代码行为的变化是唯一的因素。

换句话说,你似乎需要两个不同的测试。您应该选择运行什么测试,而不是如何运行它。

关于python - 如何在 pytest 中使用具有不同数据结构的不同 fixture 进行一次测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31276731/

相关文章:

python - BeautifulSoup 不会删除 i 元素

python - Pytest 如何包含范围为 "setup"的 "class" fixture

python - Pip freeze 给我这个与 git 相关的错误

python - 在多处理池中使用时未正确引发自定义异常

python - SQLAlchemy 的 "post_update"与已从 session 中删除的对象的行为不同

python - 我可以批量递增值以减少数据库写入吗?

Django 或 Django Rest Framework 在测试时无法解析 url 参数

python - 在 Flask-SQLAlchemy 中隔离 py.test 数据库 session

python - Py.test - 基于 session 的设置

python-3.x - 使用上下文管理器进行 Pytests