Hacker News new | ask | show | jobs
by gbog 3150 days ago
My favorite feature of pytest is pytest.mark.parametrize, which makes it easy to do table driven testing.
1 comments

what is "table driven testing"?
I've never heard of that term being used but parametrize helps in situations where:

a) You have a slew of test inputs that all need to be tested through the same function, but you don't want to duplicate code.

b) You want something like "test with every combination of [x,y,z] for parameter a and [j,k,l] for parameter b". This is probably what the grandparent is referring to.

c)You have an even more complicated scheme, which you can define.

This is better than just having a for loop over a pre-generated list of parameters and calling a function with the same assertions, because pytest sees and handles them as separate but related test cases (e.g. when reporting errors, crash handling, fixtures, etc)

Yes. But what's striking me is that one woud ever want to test only one input state. If I have to test function f(x) and see if its output is as expected, I always want to test many inputs (including "silly" ones like wrong type, nulls, extremes). Writing one test for each of them is absurd, just give a list of inputs, a list of expected outputs and check them all.