pytest: Parametrizing from function output / from directory structure

398 views Asked by At

I am trying to build testcase parameters and expected outputs from a directory structure. The directory structure is as follows:

__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

I already have written a function, let's call it makeIO(..) that takes files from each test case and returns a tuple of the format: ([input_1_contents, input_2_contents], [output_contents]).

What I am struggling with is getting that tuple to be passed into test_foo.py. Here's what I have so far:

@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

The weird part: Everything works fine and tests pass if and only if I remove the @pytest.fixture decorator from makeIO(). If I leave it there, I get this error: Fixture "makeIO" called directly. Fixtures are not meant to be called directly, but are created automatically when test functions request them as parameters.

Is there any more pythonic and elegant way of achieving what I am aiming for? Would any alternate directory structure or function be recommended? Also, I think I'm just missing the point of fixtures and would appreciate a clarification about why and how they''re used (docs didn't clarify it for me).


Disclaimer: I am completely new to pytest, so I apreciate any and all helpful resources that would flatten the learning curve.

1

There are 1 answers

0
Daniel B On

Removing @pytest.fixture from my function did exactly what I wanted, and the function can be now be parametrized though values obtained from that function.