简体   繁体   English

如何从 pytest 固定装置正确返回列表以用于参数化?

[英]How to properly return a list from a pytest fixture for use in parametrize?

I am attempting to build a little pytest test to ensure all expected keys exist in redis.我正在尝试构建一个小的 pytest 测试,以确保 redis 中存在所有预期的键。 I have a list of expected keys, that I am storing as a YML file.我有一个预期键列表,我将其存储为 YML 文件。 The test itself will query against redis to ensure each of the expected keys from the list exists.测试本身将查询 redis 以确保列表中的每个预期键都存在。

Initially, I had this set up as a giant list in the test_keys.py file.最初,我将此设置为test_keys.py文件中的一个巨大列表。 This was set up like so:这是这样设置的:

expected_keys = ['key1','key2','key3']
@pytest.mark.parametrize('expected_key', expected_keys)
def test_expected_key(expected_key):
    ...

This works.这有效。 Since I want to replicate this type of test for a few other checks of the redis environment, I don't want to put multiple lists with a few hundred keys into these files.由于我想为 redis 环境的其他一些检查复制这种类型的测试,因此我不想将包含几百个键的多个列表放入这些文件中。

I thought I could pull them out into YML files and load the keys via fixtures.我想我可以将它们提取到 YML 文件中并通过装置加载键。

My fixture looks like this:我的夹具看起来像这样:

@pytest.fixture
def expected_keys_fixture():
    with open('expected_keys.yml'), 'r') as f:
        return yaml.safe_load(f)['keys']

The YML looks like this: YML 看起来像这样:

keys:
  - key1
  - key2
  - key3

My test decorator changed to this:我的测试装饰器更改为:

@pytest.mark.parametrize("expected_keys", [
    (pytest.lazy_fixture('expected_keys_fixture'))
])
def test_expected_key(expected_key):
    ...

I am using the pytest-lazy-fixture package for this.pytest-lazy-fixture使用了pytest-lazy-fixture包。

The problem I have here is that expected_keys now equals the entire list of expected keys.我在这里遇到的问题是expected_keys现在等于预期键的整个列表。 It's no long each individual key like I had with the static list when it was in my test file.当静态列表在我的测试文件中时,它不再是每个单独的密钥。

I attempted to do as Oleh Rybalchenko answer suggested我试图按照 Oleh Rybalchenko 的回答建议去做

@pytest.mark.parametrize("expected_keys", pytest.lazy_fixture('expected_keys_fixture')
)
def test_expected_key(expected_key):
    ...

But, this fails with TypeError: 'LazyFixture' object is not iterable .但是,这失败了TypeError: 'LazyFixture' object is not iterable

I understand I only have one argname and that I should have a list, but the example in the documentation is passing the parameters to the fixture() decorator.我知道我只有一个 argname 并且我应该有一个列表,但是文档中的示例将参数传递给fixture()装饰器。 I am not.我不是。 My list is being generated by the results of the YML file.我的列表是由 YML 文件的结果生成的。


Question: How can I adjust my fixture so that it properly returned a single item at a time for the parametrize decorator?问题:如何调整我的装置,以便它一次为parametrize装饰器正确返回一个项目?

In the same situation and the only way I found is to refuse the fixture and call the function directly.在同样的情况下,我发现的唯一方法是拒绝夹具并直接调用函数。

def expected_keys_fixture():
    with open('expected_keys.yml', 'r') as f:
        return yaml.safe_load(f)['keys']


@pytest.mark.parametrize("expected_key",
                         expected_keys_fixture())
def test_expected_key(expected_key):
    assert expected_key in ['key1', 'key2', 'key3']

The problem is that fixtures are executed after collecting tests, so it's not possible to multiply you tests from fixture result.问题是夹具是收集测试执行的,因此不可能从夹具结果中乘以测试。 But you can dynamically parametrize your test with pytest_generate_tests hook .但是您可以使用pytest_generate_tests hook动态参数化您的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM