Notepad/enter/Coding Tips (Classical)/Terminal Tips/2. CLI Tools/Languages/Middle/Go/Testing in Go.md

1.4 KiB

Testing in Go

Testing seems really intuitive too. This example was given and it makes sense.


package main

import (
	"testing"
)

// LastIndex returns the index of the last instance of x in list, or
// -1 if x is not present. The loop condition has a fault that
// causes some tests to fail. Change it to i >= 0 to see them pass.
func LastIndex(list []int, x int) int {
	for i := len(list) - 1; i > 0; i-- {
		if list[i] == x {
			return i
		}
	}
	return -1
}

func TestLastIndex(t *testing.T) {
	tests := []struct {
		list []int
		x    int
		want int
	}{
		{list: []int{1}, x: 1, want: 0},
		{list: []int{1, 1}, x: 1, want: 1},
		{list: []int{2, 1}, x: 2, want: 0},
		{list: []int{1, 2, 1, 1}, x: 2, want: 1},
		{list: []int{1, 1, 1, 2, 2, 1}, x: 3, want: -1},
		{list: []int{3, 1, 2, 2, 1, 1}, x: 3, want: 0},
	}
	for _, tt := range tests {
		if got := LastIndex(tt.list, tt.x); got != tt.want {
			t.Errorf("LastIndex(%v, %v) = %v, want %v", tt.list, tt.x, got, tt.want)
		}
	}
}


And when ran, it comes out to this:

=== RUN   TestLastIndex
    prog.go:34: LastIndex([1], 1) = -1, want 0
    prog.go:34: LastIndex([2 1], 2) = -1, want 0
    prog.go:34: LastIndex([3 1 2 2 1 1], 3) = -1, want 0
--- FAIL: TestLastIndex (0.00s)
FAIL
Program exited.

whereas a passing test would say:

=== RUN   TestLastIndex
--- PASS: TestLastIndex (0.00s)
PASS
Program exited.