Skip to content

Instantly share code, notes, and snippets.

@appleboy
Created May 13, 2018 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appleboy/1ee817f8166f8fad2fb083f99e2dfe8b to your computer and use it in GitHub Desktop.
Save appleboy/1ee817f8166f8fad2fb083f99e2dfe8b to your computer and use it in GitHub Desktop.
golang testing
package car
import (
"testing"
"github.com/stretchr/testify/assert"
)
// Simple testing what different between Fatal and Error
func TestNew(t *testing.T) {
c, err := New("", 100)
if err != nil {
t.Fatal("got errors:", err)
}
if c == nil {
t.Error("car should be nil")
}
}
// Simple testing with testify tool
func TestNewWithAssert(t *testing.T) {
c, err := New("", 100)
assert.NotNil(t, err)
assert.Nil(t, c)
}
// Testing
func TestCar_SetName(t *testing.T) {
type fields struct {
Name string
Price float32
}
type args struct {
name string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "input empty name",
fields: fields{
Name: "foo",
Price: 100,
},
args: args{
name: "",
},
want: "foo",
},
{
name: "input name",
fields: fields{
Name: "foo",
Price: 100,
},
args: args{
name: "bar",
},
want: "bar",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := &Car{
Name: tt.fields.Name,
Price: tt.fields.Price,
}
if got := c.SetName(tt.args.name); got != tt.want {
t.Errorf("Car.SetName() = %v, want %v", got, tt.want)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment