草庐IT

inline-tests

全部标签

unit-testing - Go 中良好的单元测试

我一直认为好的单元测试,都是独立的测试。“独立”是指当函数“A”使用“B”时,我们测试函数“A”时,我们模拟/stub“B”以防“B”无法正常工作时它不会使“A”失败.但是当我们检查golang包的来源时,这个原则是不被尊重的。例如,让我们检查url包中的url.go和url_test.go:url.go:funcparseQuery(mValues,querystring)(errerror){forquery!=""{...key,err1:=QueryUnescape(key)...url_test.go:funcTestParseQuery(t*testing.T){fori,

unit-testing - Go 中良好的单元测试

我一直认为好的单元测试,都是独立的测试。“独立”是指当函数“A”使用“B”时,我们测试函数“A”时,我们模拟/stub“B”以防“B”无法正常工作时它不会使“A”失败.但是当我们检查golang包的来源时,这个原则是不被尊重的。例如,让我们检查url包中的url.go和url_test.go:url.go:funcparseQuery(mValues,querystring)(errerror){forquery!=""{...key,err1:=QueryUnescape(key)...url_test.go:funcTestParseQuery(t*testing.T){fori,

performance - 是否可以内联函数,在 Golang 中包含循环?

例如,我在golang中有如下测试://inline-tests.gopackageinlinetestsfuncplus(a,bint)int{returna+b}funcplus_plus(a,b,cint)int{returnplus(plus(a,b),plus(b,c))}funcplus_iter(l...int)(resint){for_,v:=rangel{res+=v}return}如果我尝试构建它,我会收到以下信息:gobuild-gcflags=-minline-tests.go#command-line-arguments./inline-tests.go:4:

performance - 是否可以内联函数,在 Golang 中包含循环?

例如,我在golang中有如下测试://inline-tests.gopackageinlinetestsfuncplus(a,bint)int{returna+b}funcplus_plus(a,b,cint)int{returnplus(plus(a,b),plus(b,c))}funcplus_iter(l...int)(resint){for_,v:=rangel{res+=v}return}如果我尝试构建它,我会收到以下信息:gobuild-gcflags=-minline-tests.go#command-line-arguments./inline-tests.go:4:

testing - 在 Go 中模拟 HTTPS 响应

我正在尝试为向Web服务发出请求的包编写测试。我遇到问题可能是因为我对TLS缺乏了解。目前我的测试看起来像这样:funcTestSimple(){server:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){w.Header().Set("Content-Type","application/json")w.WriteHeader(200)fmt.Fprintf(w,`{"fake":"jsondatahere"}`)}))transport:=&http.Transport{

testing - 在 Go 中模拟 HTTPS 响应

我正在尝试为向Web服务发出请求的包编写测试。我遇到问题可能是因为我对TLS缺乏了解。目前我的测试看起来像这样:funcTestSimple(){server:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){w.Header().Set("Content-Type","application/json")w.WriteHeader(200)fmt.Fprintf(w,`{"fake":"jsondatahere"}`)}))transport:=&http.Transport{

testing - Go 示例不会运行

我正在尝试向包中添加一个示例,并通过gotest运行该示例,但是该示例从未运行过。例如,请参阅此要点:https://gist.github.com/85469ecc65bb5bb85857要点有example_test.go:packagecow_testimport(cow"gist.github.com/85469ecc65bb5bb85857")funcExample(){cow.Poke()}然而当我运行这个时:#gotest-vexample_test.gotesting:warning:noteststorunPASSokcommand-line-arguments0.0

testing - Go 示例不会运行

我正在尝试向包中添加一个示例,并通过gotest运行该示例,但是该示例从未运行过。例如,请参阅此要点:https://gist.github.com/85469ecc65bb5bb85857要点有example_test.go:packagecow_testimport(cow"gist.github.com/85469ecc65bb5bb85857")funcExample(){cow.Poke()}然而当我运行这个时:#gotest-vexample_test.gotesting:warning:noteststorunPASSokcommand-line-arguments0.0

unit-testing - 如何测试从请求主体读取错误?

我正在为golang中的http处理程序编写单元测试。在查看此代码覆盖率报告时,我遇到了以下问题:从请求中读取请求正文时,ioutil.ReadAll可能会返回一个我需要处理的错误。然而,当我为我的处理程序编写单元测试时,我不知道如何以触发此类错误的方式向我的处理程序发送请求(内容过早结束似乎不会生成此类错误,但会在解码body)。这就是我想要做的:packagedemoimport("bytes""io/ioutil""net/http""net/http/httptest""testing")funcHandlePostRequest(whttp.ResponseWriter,r*

unit-testing - 如何测试从请求主体读取错误?

我正在为golang中的http处理程序编写单元测试。在查看此代码覆盖率报告时,我遇到了以下问题:从请求中读取请求正文时,ioutil.ReadAll可能会返回一个我需要处理的错误。然而,当我为我的处理程序编写单元测试时,我不知道如何以触发此类错误的方式向我的处理程序发送请求(内容过早结束似乎不会生成此类错误,但会在解码body)。这就是我想要做的:packagedemoimport("bytes""io/ioutil""net/http""net/http/httptest""testing")funcHandlePostRequest(whttp.ResponseWriter,r*