Table-driven tests are a good stuff


In Go there is a great emphasize on writing a table-driven tests. Main goal of these tests is to reduce copy-pasting test skeleton from test to test where only input and expected output are changed. You first define an array of test cases where each test case contains input and expected output. Then you iterate over each one in a loop where you connect test case data with functions you are testing.

I so do like utilizing them. That’s also because I’ve been fan of using this approach in PHP with PHPUnit long before I laid my eyes on Go. I’m saying this approach because I don’t think it really has a dedicated name (but in general software development this is called data-driven tests). In PHPUnit you can to this by annotation test with @dataPovider following by function name which returns array of test cases. Consult first two links below for examples for actual snippets for both sides. I think resemblance is pretty clear.

More:
PHPUnit Manual: Data Provider
Table-driven Tests [golang/go]
Data-driven tests [wikipedia]