new exporter version
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
# Comprehensive Guide to Testing in Go
|
||||
|
||||
This article will cover everything you need to know about Go testing. You will start with a simple testing function, and work through more tools and strategies to help you master testing in Go.
|
||||
|
||||
Find the tutorial [here](https://blog.jetbrains.com/go/2022/11/22/comprehensive-guide-to-testing-in-go/).
|
||||
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Fooer If the number is divisible by 3, write "Foo" otherwise, the number
|
||||
func Fooer(input int) string {
|
||||
|
||||
isfoo := (input % 3) == 0
|
||||
|
||||
if isfoo {
|
||||
return "Foo"
|
||||
}
|
||||
|
||||
return strconv.Itoa(input)
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
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"})
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
module testing-guide
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/stretchr/testify v1.8.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/objx v0.5.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
Reference in New Issue
Block a user