learning_go/testing/go-code-samples/testing-guide/fooer_test.go
2025-12-20 17:00:05 +03:00

106 lines
2.2 KiB
Go

package main
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestFooer(t *testing.T) {
result := Fooer(3)
if result != "Foo" {
t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
}
}
func TestFooer1(t *testing.T) {
type args struct {
input int
}
tests := []struct {
name string
args args
want string
}{
{"9 should be Foo", args{9}, "Foo"},
{"3 should be Foo", args{3}, "Foo"},
{"1 is not Foo", args{1}, "1"},
{"0 should be Foo", args{0}, "Foo"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Fooer(tt.args.input); got != tt.want {
t.Errorf("Fooer() = %v, want %v", got, tt.want)
}
})
}
}
func TestFooer2(t *testing.T) {
input := 3
result := Fooer(3)
t.Logf("The input was %d", input)
if result != "Foo" {
t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
}
t.Fatalf("Stop the test now, we have seen enough")
t.Error("This won't be executed")
}
func TestFooerParallel(t *testing.T) {
t.Run("Test 3 in Parallel", func(t *testing.T) {
t.Parallel()
result := Fooer(3)
if result != "Foo" {
t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
}
})
t.Run("Test 7 in Parallel", func(t *testing.T) {
t.Parallel()
result := Fooer(7)
if result != "7" {
t.Errorf("Result was incorrect, got: %s, want: %s.", result, "7")
}
})
}
func TestFooerSkipped(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
result := Fooer(3)
if result != "Foo" {
t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
}
}
func BenchmarkFooer(b *testing.B) {
for i := 0; i < b.N; i++ {
Fooer(i)
}
}
func TestFooerWithTestify(t *testing.T) {
// assert equality
assert.Equal(t, "Foo", Fooer(0), "0 is divisible by 3, should return Foo")
// assert inequality
assert.NotEqual(t, "Foo", Fooer(1), "1 is not divisible by 3, should not return Foo")
}
func TestMapWithTestify(t *testing.T) {
// require equality
require.Equal(t, map[int]string{1: "1", 2: "2"}, map[int]string{1: "1", 2: "3"})
// assert equality
assert.Equal(t, map[int]string{1: "1", 2: "2"}, map[int]string{1: "1", 2: "2"})
}