Added table-driven tests and comments

This commit is contained in:
Eli Bendersky 2019-09-12 11:23:01 -07:00
parent e5af060488
commit 507f2b6145

View File

@ -1,9 +1,23 @@
// Unit testing is an important part of writing
// principled Go programs. The `testing` package
// provides the tools we need to write unit tests
// and the `go test` command runs tests.
// For the same of demonstration, this code is in package
// `main`, but it could be any package. Testing code
// typically lives in the same package as the code it tests.
package main
import (
"fmt"
"testing"
)
// We'll be testing this simple implementation of an
// integer minimum. Typically, the code we're testing
// would be in a source file named something like
// `intutils.go`, and the test file for it would then
// be named `intutils_test.go`.
func IntMin(a, b int) int {
if a < b {
return a
@ -12,9 +26,43 @@ func IntMin(a, b int) int {
}
}
// A test is created by writing a function with a name
// beginning with `Test`.
func TestIntMinBasic(t *testing.T) {
result := IntMin(2, -2)
if result != -2 {
// `t.Error*` will report test failures but continue
// executing the test. `t.Fail*` will report test
// failures and stop the test immediately.
t.Errorf("IntMin(2, -2) = %d; want -2", result)
}
}
// Writing tests can be repetitive, so it's idiomatic to
// use a *table-driven style*, where test inputs and
// expected outputs are listed in a table and a single loop
// walks over them and performs the test logic.
func TestIntMinTableDriven(t *testing.T) {
var tests = []struct {
a, b int
expected int
}{
{0, 1, 0},
{1, 0, 0},
{2, -2, -2},
{0, -1, -1},
{-1, 0, -1},
}
for _, tt := range tests {
// t.Run enables running "subtests", one for each
// table entry. These will be reported separately
// when executing `go test -v`.
t.Run(fmt.Sprintf("%d,%d", tt.a, tt.b), func(t *testing.T) {
result := IntMin(tt.a, tt.b)
if result != tt.expected {
t.Errorf("got %d, want %d", result, tt.expected)
}
})
}
}