草庐IT

go - 如何从 *http.Request 和 *httptest.ResponseRecorder 创建 gin.Context?

我想为gin创建一个测试助手类似于testify'sHTTPBodyContain.我很难从*http.Request和*httptest.ResponseRecorder创建gin.Context。我已经写过这样的东西:funcHTTPBodyContains(t*testing.T,handlergin.HandlerFunc,method,urlstring,valuesurl.Values,strinterface{},msgAndArgs...interface{})bool{body:=HTTPBody(handler,method,url,values)contains:

unit-testing - 以 http 作为包装函数的依赖项的单元测试

我有以下函数,在@poy的帮助下,我能够为它创建模拟以便对其进行单元测试。现在的问题是我有包装函数也需要测试这是原始函数,已经过测试funchttpReq(cc[]string,methodstring,urlstring)([]byte,error){httpClient:=http.Client{}req,err:=http.NewRequest(method,url,nil)iferr!=nil{returnnil,errors.Wrap(err,"failedtoexecutehttprequest")}//Herewearepassinguserandpasswordreq.

http - 用户存在时服务器返回 400

我正在用Go进行一些测试,过去2天我一直在努力让它工作,但我做不到。我的问题是,即使用户确实存在,测试也会返回400。这是我的getUser函数func(handler*UserHandler)getUser(whttp.ResponseWriter,IDint){logfile,err:=os.OpenFile("events.log",os.O_RDWR|os.O_CREATE|os.O_APPEND,0666)iferr!=nil{log.Fatalf("Erroropeningfile:%v",err)}deferlogfile.Close()log.SetOutput(log

unit-testing - 如何在 ReverseProxy 中测试 header ?

我正在尝试对以下代码进行单元测试:func(h*Handler)Forward(whttp.ResponseWriter,r*http.Request){url,err:=url.Parse("http://test.com")iferr!=nil{return}reverseProxy:=&httputil.ReverseProxy{Director:func(r*http.Request){r.URL.Host=url.Hostr.URL.Path="/"r.URL.Scheme=url.Schemer.Host=url.Hostr.Header.Set("X-Forwarded-

go - 如何在运行 'go test' 时排除或跳过特定目录

这个问题在这里已经有了答案:Runningtestsandskippingsomepackages(1个回答)关闭3年前。gotest$(golist./...|grep-v/vendor/)-coverprofile.testCoverage.txt我正在使用上面的命令来测试文件,但是我想从测试中排除1个名为“Store”的文件夹。怎么做到的?

go - 如何在保留测试可比性的同时使用动态误差?

在go中我经常用funcMyFunc(ssomeInterface)error{err:=OtherFunc(s)returnfmt.Errorf("somethingwrong:%s",err)}所以我失去了原来的错误值,因为我只是把错误字符串伪造成一个新的错误。这就是我所说的动态错误。现在考虑对MyFunc()进行测试:funcTestMyFunc(t*testing.T){s:=mockSomeInterface()testErr:=MyFunc(s)iftestErr!=interfaceSpecificErrorValue{t.Errorf("fail")}}interfac

unit-testing - 表驱动测试中的竞争检测

我有一个表驱动测试和表循环外的initvar(worker)。我使用gotest-raсe运行测试并添加了t.Parallel()并且没有检测到竞争条件。我可以假设我的测试没有竞争条件吗://Thismockcouldbeinaseparatefile.typemockWorkerstruct{}//implmentourWorkeriterfacefunc(mdmockWorker)Work()error{returnnil}typemockDoerstruct{ErrorerrorWorkerworker}//implmentourDoeriterfacefunc(mdmockDo

unit-testing - 单元测试调用另一个方法的方法

对调用多个方法的方法进行单元测试的最佳方法是什么,例如:modify(stringvalue){if(value.Length>5)replaceit(value);elsechangeit(value);}此伪代码有一个修改方法,(当前)调用replaceit()或changeit()。我已经为replaceit和changeit编写了测试,因此为modify编写新测试将是99%的同一组代码。我需要测试它的想法,因为它可能会在未来发生变化。那么我是否复制粘贴现有的测试代码?将测试代码移动到一个通用函数?还有其他想法吗?我不确定此处的最佳做法。 最佳答案

unit-testing - 使用 github.com/jarcoal/httpmock 验证请求主体

我正在尝试测试的函数接受数据结构,形成一个query_dsl,然后使用形成的查询对Elasticsearch进行/_search调用。因此,我想对形成的query_dsl和url进行断言。我正在使用github.com/jarcoal/httpmock在我的单元测试中模拟net/http请求。根据文档,它公开了funcGetCallCountInfo()map[string]int以验证特定端点被命中的次数。但我也有兴趣知道进行此调用时请求正文是什么。http.Client未公开,因此无法覆盖/模拟它进行测试。如果无法使用此包,那么是否有任何其他库可以模拟网络请求并提供请求主体?

testing - 如何按顺序运行 golang 测试?

当我运行gotest时,我的输出:---FAIL:TestGETSearchSuccess(0.00s)Location:drivers_api_test.go:283Error:Notequal:200(expected)!=204(actual)---FAIL:TestGETCOSearchSuccess(0.00s)Location:drivers_api_test.go:391Error:Notequal:200(expected)!=204(actual)但是在我再次运行gotest之后,我的所有测试都通过了。仅当我重置我的mysql数据库,然后第一次运行gotest时,测试