assert.EqualValues(t, http.StatusOK, w.Code) got, _ := strconv.Atoi(w.Body.String()) assert.Equal(t,1, got) }//mock gin contextfuncGetTestGinContext(w *httptest.ResponseRecorder) *gin.Context{ gin.SetMode(gin.TestMode) ctx, _ := gin.CreateTestContext(w) ctx.Request= &http.Requ...
assert.Equal(t, tt.want, got) assert.Equal(t, tt.wantErr, err != nil) } } 分析代码生成测试用例 对checkUsername、checkEmail纯逻辑函数编写测试用例,这里以checkEmail为例。 func Test_checkEmail(t *testing.T) { type args struct { email string } tests := []struct { name string args args...
TestMain 运行在主 goroutine 中, 可以在调用 m.Run 前后做任何设置和拆卸。注意,在 TestMain 函数的最后,应该使用 m.Run 的返回值作为参数调用 os.Exit。 详情参见:TestMain https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter09/09.5.html#testmain 1.3 httptest——HTTP测试辅...
gocheck 是 golang 语言比较健全且简单的单元测试框架,gocheck 在 golang 官方的testing package 之上,丰富了很多功能,丰富了单元测试常用的 assert 断言,判断动词deep multi-type 对比,字符串比较以及正则匹配。测试用例组织集合方面按suite组织测试用例,支持suite级别的 setup() 和 teardown()。对于临时文件支持创建...
使用testify/assert 简化条件判断 上面例子中很多 if xxx { t.Errorf(...) } 的代码,复杂,语义不清晰。使用stretchr/testify的 assert 可以简化这些代码。上面的 for 循环可以简化成下面这样: import"github.com/stretchr/testify/assert"forrow,test:=rangetests{r,err:=Mod(test.a,test.b)iftest.hasError{...
"go-demo/m/unit-test/entity" "xorm.io/xorm" ) type UserRepo interface { AddUser(ctx context.Context, user *entity.User) (err error) DelUser(ctx context.Context, userID int) (err error) GetUser(ctx context.Context, userID int) (user *entity.User, exist bool, err error) ...
在当前路径下执行go test命令,可以看到输出结果如下: ❯ go testPASSok golang-unit-test-demo/base_demo 0.005s go test -v 一个测试用例有点单薄,我们再编写一个测试使用多个字符切割字符串的例子,在split_test.go中添加如下测试函数: ...
// gin_test.go package httptest_demo import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "/stretchr/testify/assert" ) func Test_helloHandler(t *testing.T) { // 定义两个测试用例 tests := []struct {
engine.GET("/api/test/code_review/repo", ListRepoCrAggregateMetrics) req, _ := http.NewRequest("GET","/api/test/code_review/repo?work_no=999999", nil) engine.ServeHTTP(w, req) assert.Equal(t, w.Code,200)varv map[string]RepoCrMetricsRsp ...
func TestLiveAPI(t*testing.T){iftesting.Short(){ t.Skip("skipping integration test")} client :=NewClient("https://api.example.com",5*time.Second)t.Run("GetExistingUser",func(t*testing.T){user,err :=client.GetUser(context.Background(),1)require.NoError(t,err)assert.Equal(t,int64(...