Skip to content

Instantly share code, notes, and snippets.

@ynwd
Last active December 23, 2020 10:18
Show Gist options
  • Select an option

  • Save ynwd/764841fa364dbc8efb5c588d68f75343 to your computer and use it in GitHub Desktop.

Select an option

Save ynwd/764841fa364dbc8efb5c588d68f75343 to your computer and use it in GitHub Desktop.
golang unit test with vscode
  1. create salam.go
  2. create main.go
  3. open salam.go on vs-code
  4. put your pointer to CreateSalam method.
  5. righ-click, select 'Generate Tests for Function'. this will create a new file salam_test.go
  6. run go test ./...
package main
func main() {
o := Salam{}
o.CreateSalam("pagi")
}
package main
// Salam ..
type Salam struct {
}
// CreateSalam create salam
func (s *Salam) CreateSalam(waktu string) string {
if waktu == "pagi" {
return "pagi"
}
return "hello"
}
package main
import "testing"
func TestSalam_CreateSalam(t *testing.T) {
type args struct {
waktu string
}
tests := []struct {
name string
s *Salam
args args
want string
}{
{
name: "pagi",
s: &Salam{},
args: args{"pagi"},
want: "pagi",
},
{
name: "pagi",
s: &Salam{},
args: args{"malam"},
want: "hello",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Salam{}
if got := s.CreateSalam(tt.args.waktu); got != tt.want {
t.Errorf("Salam.CreateSalam() = %v, want %v", got, tt.want)
}
})
}
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment