- create
salam.go - create
main.go - open
salam.goon vs-code - put your pointer to
CreateSalammethod. - righ-click, select 'Generate Tests for Function'. this will create a new file
salam_test.go - run
go test ./...
Last active
December 23, 2020 10:18
-
-
Save ynwd/764841fa364dbc8efb5c588d68f75343 to your computer and use it in GitHub Desktop.
golang unit test with vscode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| func main() { | |
| o := Salam{} | |
| o.CreateSalam("pagi") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| // Salam .. | |
| type Salam struct { | |
| } | |
| // CreateSalam create salam | |
| func (s *Salam) CreateSalam(waktu string) string { | |
| if waktu == "pagi" { | |
| return "pagi" | |
| } | |
| return "hello" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment