From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Table-driven testing

Table-driven testing

- Table driven tests can be used to cover a lot of ground quickly while reusing common setup and comparison code. Table driven testing is not unique in GO. However, it is very popular in the GO community. Here we can use the GO tool cover command and the dash funk flag to generate a coverage report by function. We can see the code coverage shows that the all function has several code paths that are not currently covered by the tests. We can use table driven tests to help increase code coverage. Here we have an example of a way that a table driven test is structured. We use a slice of anonymous structs and the anonymous structs will define any fields needed for each test case. Then we will fill that slice with the test cases we want to test. Finally, we loop through the slice of test cases and perform the test for each case provided. Let's use table driven test to improve the code coverage of the all method on the store type. First, we need to set up our common test data. For example…

Contents