python - 在 pytest.mark.parametrize 中使用 fixture

标签 python python-2.7 testing pytest

假设我有一个测试函数,它将参数化 record 作为字典,其中一个值是一个已经定义的 fixture 。

例如,我们有一个 fixture :

@pytest.fixture
def a_value():
    return "some_value"

和测试函数:

@pytest.mark.parametrize("record", [{"a": a_value, "other": "other_value"},
                                    {"a": a_value, "another": "another_value"}])
def test_record(record):
    do_something(record)

现在,我知道这可以通过将 fixture 传递给测试函数并相应地更新记录来解决,例如:

@pytest.mark.parametrize("record", [{"other": "other_value"},
                                    {"another": "another_value"}])
def test_record(a_value, record):
    record["a"] = a_value
    do_something(record)

但我想知道是否有一种方法可以在没有这种“变通办法”的情况下做到这一点,当我有许多已经定义的固定装置并且我只想在我传递给函数的每个参数化记录中使用它们时。

我已经查过了this question ,虽然它似乎并不完全适合我的情况。无法从那里的答案中找到正确的用法。

最佳答案

一个解决方案是创建 record 作为固定装置而不是使用 parametrize 并接受 a_value 作为参数:

@pytest.fixture
def record(a_value):
    return {
        'a': a_value,
        'other': 'other_value',
    }

关于python - 在 pytest.mark.parametrize 中使用 fixture ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54078436/

相关文章:

ruby-on-rails - 无法在 sign_out 方法的功能测试中销毁 current_user

python - 如果列值大于其他值,如何在python中创建累积总和列

python - 如何检索列表中具有特殊字符的特定字段

python - 如何处理 Python Bottle 和 Apache .htaccess 中的 URL 重新路由?

python - 在鼠标拖动 Pyglet 周围绘制一个矩形

python - 查找匹配空值和 null 值的 ElasticSearch 记录

unit-testing - 测试 Sencha touch 2 应用程序

python - mysqldump 的奇怪十六进制字符行为

python-2.7 - 如何将参数传递给数据流模板以进行管道构建

ruby-on-rails - Rails 4 - 所有测试都失败(甚至断言 true)