Testing

Programming is not easy; even the best programmers are incapable of writing programs that work exactly as intended every time. Therefore an important part of the software development process is testing. Writing tests for our code is a good way to ensure quality and improve reliability.

Go includes a special program that makes writing tests easier, so let's create some tests for the package we made in the last chapter. In the math folder from chapter11 create a new file called math_test.go that contains this:

The go test command will look for any tests in any of the files in the current folder and run them. Tests are identified by starting a function with the word Test and taking one argument of type *testing.T. In our case since we're testing the Average function we name the test function TestAverage.

Once we have the testing function setup we write tests that use the code we're testing. In this case we know the average of [1,2] should be 1.5 so that's what we check. It's probably a good idea to test many different combinations of numbers so let's change our test program a little:

This is a very common way to setup tests (abundant examples can be found in the source code for the packages included with Go). We create a struct to represent the inputs and outputs for the function. Then we create a list of these structs (pairs). Then we loop through each one and run the function.

Problems

Writing a good suite of tests is not always easy, but the process of writings tests often reveals more about a problem then you may at first realize. For example, with our Average function what happens if you pass in an empty list ([]float64{})? How could we modify the function to return 0 in this case?

Write a series of tests for the Min and Max functions you wrote in the previous chapter.