【问题标题】:What does indirect = True/False in pytest.mark.parametrize do/mean?pytest.mark.parametrize 中的间接 = True/False 是什么意思?
【发布时间】:2016-05-26 14:03:11
【问题描述】:

我只是想了解如果我在 pytest.mark.parametrize 中将间接参数设置为 True 或 False 是什么意思或会发生什么?

谢谢

【问题讨论】:

标签: python pytest


【解决方案1】:

使用indirect=True,您可以参数化您的灯具,False - 默认值。示例:

import pytest

@pytest.fixture
def fixture_name(request):
    return request.param

@pytest.mark.parametrize('fixture_name', ['foo', 'bar'], indirect=True)
def test_indirect(fixture_name):
    assert fixture_name == 'baz'

所以这个例子生成了两个测试。第一个来自fixture_name 值foo,因为这个测试的fixture 使用参数化运行。第二个测试获得 bar 值。并且每个测试都会失败,因为断言检查 baz。

【讨论】:

  • indirect=True 确保您在测试中使用参数化时仍然执行夹具主体。
  • 所以indirect 是一种参数化夹具的方法。与使用 pytest.fixture(params=[...]) 参数化夹具相比的主要区别在于,夹具只能针对特定的测试功能进行参数化(否则它总是为使用夹具的所有测试功能进行参数化)?
  • 在这种情况下你会如何断言回报?
  • 我刚刚发现有必要将夹具的参数(将接受参数)称为request。其他名称将不起作用,pytest 会抱怨该其他名称的夹具不存在。 pytest 6.2.2
猜你喜欢
  • 2019-08-12
  • 2021-08-17
  • 2014-11-05
  • 2018-03-03
  • 2021-12-10
  • 2016-08-19
  • 2015-07-11
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多