Hacker News new | past | comments | ask | show | jobs | submit login

Things I find awkward with unit test (and it might just be me) is Inwant to write a test like

def testLotsOfWaysTofail(): d = {"Handle Null": (None, foobar), "Don't allow under 13 to do it": (11, foobar), "or old age pensioner": (77,wobble)} for ... generate a unit test dynamically here

I have built metaclasses, I have tried many different options. I am sure there is a near solution.

But I never seem to have it right




What you are looking for is called property based testing


isn't this just a parametrized test? or do you want to generate the test cases automatically like in property-based testing?


Yes parameterised testing. Just all the packages i have tried seemed to make it awkward to impossible - hence metaclasses


Does this do what you want?

https://github.com/ckp95/pytest-parametrize-cases

    @parametrize_cases(
        Case("handle null", age=None, x="foobar"),
        Case("don't allow under 13s", age=11, x="foobar"),
        Case("or old age pension", age=77, x="wobble"),
        ... # as many as you want
    )
    def test_lots_of_ways_to_fail(age, x):
        with pytest.raises(ValueError):
            function_under_test(age, x)


Just wanted to pop in and say ckp95 actually mailed me this reply in case I missed it. The extra effort kinda restored my faith in humanity - giving a shit about strangers problems matters these days. Nice one.

cf https://youtu.be/wUyI3ssmj_U




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: