0

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

__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.fixturemakeIO(). 如果我把它留在那里,我会收到这个错误: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 完全陌生,因此我感谢所有可以使学习曲线变平的有用资源。

4

1 回答 1

0

从我的函数中删除@pytest.fixture正是我想要的,现在可以通过从该函数获得的值对函数进行参数化。

于 2020-10-23T14:16:54.567 回答