How to generate golang unit test code in VS code
vinay
Posted on October 21, 2022
- Go for Function name --->right click and in that we have so many option--->select --go: generate unit test for function
main.go
package main
import "fmt"
func Add(a int, b int) int {
return a + b
}
func main() {
b := Add(10, 20)
fmt.Println(b)
}
main_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
name string
args args
want int
}{
{
name: "hello",
args: args{
a: 1,
b: 2,
},
want: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Add(tt.args.a, tt.args.b); got != tt.want {
t.Errorf("Add() = %v, want %v", got, tt.want)
}
})
}
}
💖 💪 🙅 🚩
vinay
Posted on October 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024