草庐IT

$AB测试

全部标签

unit-testing - 如何为一小时后函数返回时间编写单元测试

我有以下函数,它在当前时间添加给定的小时数并在中返回新时间funcgetExpiryTime(hourint)*string{constlayout="2006-01-02T15:04:05Z"expiryTime:=time.Now().Local().Add(time.Hour*time.Duration(hour))returnaws.String(expiryTime.Format(layout))}为此功能编写单元测试的最佳方法是什么? 最佳答案 您可以尝试模拟时间提供程序并在您的模拟中设置time.Now()函数以返回您

Golang 基准测试 : why does allocs/op show 0 B/op?

这是基准测试的代码片段://bench_test.gopackagemainimport("testing")funcBenchmarkHello(b*testing.B){fori:=0;i指标allocs/op显示0B/op。变量a是一个int类型,不会占用太多内存,但它不应该占用零B.>gotest-bench=.-benchmemgoos:darwingoarch:amd64pkg:aBenchmarkHello-420000000000.26ns/op0B/op0allocs/opPASSoka0.553s 最佳答案 Wh

unit-testing - 去单元测试无法加载文件

1、gotestconfig_test.go,总是文件,error:openconf/config.yml:Thesystemcannotfindthepathspecified2、源码ok3、gotestfile如何在不同目录下成功 最佳答案 测试期间您的工作目录不同。您可能需要动态计算配置目录路径的解决方法,例如:http://www.kaihag.com/external-assets-working-directories-and-go/ 关于unit-testing-去单元测试

go - 由于未接来电而导致测试中止

我试图在本地函数内部调用的方法上mock.Expect。当我通过考试时。但当我期望它至少执行一次时,它失败了。此调用之前绝对没有其他代码路径,因此不应失败。我在这里使用方法的简化表示,因为这是一个复杂的测试设置/方法。但这绝对是要点。我的问题是,我的测试是否因为我模拟的方法而失败。期望的不是直接在mock.Expect.AddNumbers(a,b).AnyTimes().Return(&result.sum,nil)函数内部?add_test.goimportresultTestAdditionFunction(t*testing.T){//mocksetupstuff...a:=1

go - 作为一个单元测试用例,应该为这样的函数编写什么。代码片段会有所帮助

funcHome(whttp.ResponseWriter,r*staticAuth.AuthenticatedRequest){t,err:=template.ParseFiles("index.html")//parsethehtmlfilehomepage.htmliferr!=nil{//ifthereisanerrorlog.Print("templateparsingerror:",err)//logit}err=t.Execute(w,nil)//executethetemplateandpassittheHomePageVarsstructtofillinthegaps

unit-testing - 如何断言 Go 测试中的错误类型?

我有一个如下定义的错误类型typeRetryableErrorstruct{msgstring}func(a*RetryableError)Error()string{returna.msg}在单元测试中,如果返回的错误是RetryableError类型,Go的断言方式是什么? 最佳答案 使用类型断言:err:=someFunc()ifretryable,ok:=err.(RetryableError);ok{//useretryable}您的RetryableError不是错误,但*RetryableError是。更正:func(

unit-testing - 我如何测试代码是否真的启动了一个 go 例程?

如果我在我的代码中创建一个go例程,我该如何编写一个测试用例来测试是否正在创建一个goroutine。 最佳答案 howdoIwriteatestcasethattestswhetheragoroutineisbeingcreated这绝不是您要寻找的。相反,您应该对测试进行检测,以检查go例程应该执行的操作是否确实发生了。 关于unit-testing-我如何测试代码是否真的启动了一个go例程?,我们在StackOverflow上找到一个类似的问题: htt

go - 我将如何对 golang 中的多个方法进行负载测试(vegeta)

假设我有一个api有两条路线,一条是用于保存用户,另一条是用于获取下面给出的用户:-路由器.gopackagemainimport("github.com/gin-gonic/gin""go-training/postAPI/controller")typeRoutestruct{NamestringMethodstringPatternstringHandlerFuncfunc(*gin.Context)}typeRoutes[]Routevarroutes=Routes{Route{"SaveUser","POST","/post",controller.SaveUser},Rou

go - 了解测试覆盖率

我的Go程序中有一个简单的包来生成哈希ID。我也为它编写了一个测试,但无法理解为什么我只覆盖了83%的语句。下面是我的包功能代码:packagehashgenimport("math/rand""time""github.com/speps/go-hashids")//GenHashfunctionwillgenerateauniqueHashusingthecurrenttimeinUnixepochformatastheseedfuncGenHash()(string,error){UnixTime:=time.Now().UnixNano()IntUnixTime:=int(Un

go - net/http 服务器在 ab (ApacheBench) 重负载下卡住

MacOS上的Golangnet/http服务器在16000个请求后卡住:$ab-c4-n20000http://127.0.0.1:8080/ThisisApacheBench,Version2.3Copyright1996AdamTwiss,ZeusTechnologyLtd,http://www.zeustech.net/LicensedtoTheApacheSoftwareFoundation,http://www.apache.org/Benchmarking127.0.0.1(bepatient)Completed2000requestsCompleted4000reque