oprypin / pytest-golden

Plugin for pytest that offloads expected outputs to data files
MIT License
23 stars 2 forks source link

Can this be used to test actual about given varied inputs #4

Closed reagle closed 8 months ago

reagle commented 1 year ago

Presently I have the following code, which is run as part of a doctest. I thought pytest and -golden might be able to do the same -- in a more standard manner. However, I'm a pytest newbie and not sure how and the input and output files aren't actual program input and output files. Can I test those with this module?

def _test_results():
    """
    Tests the overall parsing of Mindmap XML and the relationships between
    authors with multiple titles and nested authors.

    >>> call('thunderdell.py -i ~/bin/td/tests/author-child.mm > \
    /tmp/author-child.yaml; \
    diff ~/bin/td/tests/author-child.yaml /tmp/author-child.yaml', shell=True)
    0
    >>> call('thunderdell.py -i ~/bin/td/tests/author-descendent.mm > \
    /tmp/author-descendent.yaml; \
    diff ~/bin/td/tests/author-descendent.yaml /tmp/author-descendent.yaml', \
    shell=True)
    0
    """  # noqa: E501
oprypin commented 1 year ago

I'm not familiar with the pytest ecosystem. What you're describing sounds like "doctest". pytest-golden definitely doesn't look at docstrings and wouldn't be able to automatically update them. The thing it does is just update yaml files pretty much.

reagle commented 1 year ago

@oprypin, hi! Thanks for the response. I want to move away from doctests. I want to test the output of a program against a given "golden" output and was wondering if there was a way to do that with this plugin. However, it appears the files in this program have there own format (e.g., "input:") and are not actual program output?

oprypin commented 1 year ago

Ah sure you could have input be the command and output be the program output. That works well.

reagle commented 1 year ago

But don't the input and output files expect input: and output: as their first lines respectively?

Lythenas commented 8 months ago

No you can use any field you like. You just need to reference it correctly.

E.g.

a: 1
b: 2
@pytest.mark.golden_test("*.yml")
def test_add(golden):
    assert golden["a"] + 1 == golden.out["b"]

Just use golden like a normal dictionary (loaded from the yaml) for the input and use golden.out like a normal dictionary (parsed from the yaml) for the expected output. The reason to use golden.out for the expected output is so that the yaml can be automatically updated.