pytest:从函数输出/从目录结构进行参数化

问题描述

我正在尝试从目录结构构建测试用例参数和预期的输出。目录结构如下:

__init__.py
foo.py
testing/
├── test_foo/
│   ├── __init__.py
│   ├── case-0001/
│   │   ├── input_1.json
│   │   ├── intput_2.json
│   │   └── output.json
│   ├── case-0002/
│   │   ├── input_1.json
│   │   ├── intput_2.json
│   │   └── output.json
│   └── test_fooFunc.py

我已经写了一个函数,我们称其为 makeIO(..) ,该函数从每个测试用例中获取文件,并返回格式为([input_1_contents,input_2_contents],[output_contents])元组

我正在努力的是将元组传递给 test_foo.py 。这是我到目前为止的内容

@pytest.fixture
def makeIO():
    ...
    return IOtuple

IOtuple = makeIO(..)


@pytest.mark.parametrize("test_input,expected",IOtuple)
def test_two(test_input,expected):
    assert foo.fooFunc(test_input) == expected

奇怪的部分:一切正常,并且测试通过了,并且仅当我从删除 @pytest.fixture 装饰器时> makeIO() 。如果我将其保留在此处,则会出现以下错误Fixture "makeIO" called directly. Fixtures are not meant to be called directly,but are created automatically when test functions request them as parameters.

是否有更多的Pythonic和优雅的方式来实现我的目标?是否建议使用任何其他目录结构或功能?另外,我想我只是缺少固定装置的要点,希望对使用它们的原因和方式进行澄清(文档没有为我澄清)。


免责声明:我对pytest完全陌生,因此,我对所有和所有有用的资源都会感到满意,这将使学习曲线变得平坦。

解决方法

从我的函数中删除@pytest.fixture完全符合我的要求,现在可以通过从该函数获得的值对其进行参数化。