Skip to content

Suggestion: provide subtest kwargs within test context #204

@k2bd

Description

@k2bd

A common pattern I come across in writing tests with subtests is something like this:

def test_my_function(subtests: SubTests):
    do_some_setup() 

    with subtests.test(a=1, b="abcde", c=None):
        result = my_function(a=1, b="abcde", c=None)
        assert result == ... 

    with subtests.test(a=99, b="something else", c=datetime.date(2021, 2, 3)):
        result = my_function(a=99, b="something else", c=datetime.date(2021, 2, 3))
        assert result == ...

i.e. I'll have subtests that are set up to report on the console what arguments I'm testing against, and then I'll call the function with those arguments and assert the results.

This leads to some repetition and minor scope for human error when copying out these subtests and matching the kwargs reported to the kwargs used in the subtest. It's not a big deal, and of course there are other alternatives, like defining the kwargs before the subtest and passing them to both subtests.test(**kwargs) and my_function(**kwargs). But I think it'd be cool to be able to do something like:

def test_my_function(subtests: SubTests):
    do_some_setup() 

    with subtests.test(a=1, b="abcde", c=None) as subtest:
        result = my_function(**subtest.kwargs)
        assert result == ... 

    with subtests.test(a=99, b="something else", c=datetime.date(2021, 2, 3)) as subtest:
        result = my_function(**subtest.kwargs)
        assert result == ...

i.e. have _SubTestContextManager.__enter__ return an object with information about the subtest that could optionally be used to remove duplication in uses like this.

Is this something that might be useful and is in-scope for this plugin? I'd be happy to work on an MR with a proposal if so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions