草庐IT

monkey-testing

全部标签

unit-testing - 在 golang 中实现嵌套接口(interface)

我有一个包含很多子方法的接口(interface)。typeInterfaceCheckoutinterface{GetID()intGetItems()[]InterfaceCartItem//...thenlike30moremethods}我有一个只使用GetItems方法的方法。funcGetRates(checkoutIInterfaceCheckout)[]Rate{for_,item:=rangecheckoutI.GetItesm(){p:=item.GetProduct()}}我希望能够测试此GetRates方法,而无需模拟InterfaceCheckout中的所有方

unit-testing - 在 Go 中为 httptest.NewServer 使用自定义 URL

我通过在Go语言中创建一个http测试服务器来在一些rest调用上运行UT。我的代码如下。typestudentstruct{FirstNamestringLastNamestring}functestGetStudentName(){testServer:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){response=new(student)response.FirstName="Some"response.LastName="Name"b,err:=json.Marsha

unit-testing - 是否可以在 go test 中跨单元测试共享上下文?

我正在Go中实现一个trie作为学习语言的一种方式。我想从一开始就编写测试,以此来感受Go的处理方法。我在测试trie时遇到的一个问题是我必须为每个单元测试重建它。有没有办法在单元测试中重用我的特里树的相同实例?理想情况下,我想要一种无需任何外部依赖项即可执行此操作的方法。 最佳答案 是的:只需在trie_test.go的funcinit()中构造它。(或者甚至使用文字。) 关于unit-testing-是否可以在gotest中跨单元测试共享上下文?,我们在StackOverflow上找

go - 为什么当我运行 `go test` 时这个函数未定义?

这是代码去吧packagemainimport"fmt"funcHaha(){fmt.Println("inHaha")}funcmain(){}a_test.gopackagemain_testimport"testing"funcTestA(t*testing.T){Haha()}gobuild有效。但是当我运行~/gopath/src/zjk/misc$gotest-v时。这是我得到的#zjk/misc_test./a_test.go:6:undefined:HahaFAILzjk/misc[buildfailed] 最佳答案

testing - 有没有办法将断言与作证联系起来?

我真的很喜欢testify为gotest带来的东西。但是,我仔细阅读了文档,但没有看到有关如何处理多个断言的任何内容。Go是否处理“第一次失败”,即它在第一个错误断言处失败,或者它只关心测试方法中的最后一个断言? 最佳答案 您可以使用testify/require,它具有与assert完全相同的接口(interface),但它会在失败时终止执行。http://godoc.org/github.com/stretchr/testify/requireimport("testing""github.com/stretchr/testif

unit-testing - 标准库错误的覆盖率测试

我一直在尝试了解Go的内置测试框架并获得适当的测试覆盖率。在我正在测试的其中一个文件中,我只获得了大约87%的覆盖率:覆盖率:87.5%的语句下面是测试中的部分代码://Checkthattheworkingdirectoryissetif*strctFlags.PtrWorkDir==""{//iftheworkingdirectoryisempty,setittothecurrentdirectorystrTemp,err:=os.Getwd()iferr!=nil{return"",errors.New("Couldnotgetcurrentworkingdirectory")}

testing - 如何测试延迟的 Go 语句?

如何测试doStuff函数?(Playground:http://play.golang.org/p/aPFSlaBLgX)packagemyPackagevarlocked=falsefuncdoStuff(){deferunlock()lock()//sometaskthatcancauseerrors//needtotestiflockwasreallyunlocked//thisisjustasimpleexample,thingscangocomplexonrealworldpanic("!")}funclock(){locked=true}funcunlock(){lock

go - 使用 go test 时出现身份验证错误

我在设置exportGO111MODULE=on后使用gotest命令更新go.mod并运行测试套件。我看到很多像下面这样的身份验证错误Theauthenticityofhost'gitlab.com(35.231.145.151)'can'tbeestablished.ECDSAkeyfingerprintisSHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.Areyousureyouwanttocontinueconnecting(yes/no)?Theauthenticityofhost'gitlab.com(35.231.14

testing - 如何使用相对目录测试 Go 程序?

来自测试文档:“该包构建在一个临时目录中,因此它不会干扰非测试安装。”因此,任何使用与可执行文件相关的目录的代码都不会相对于Temp\go-build出现......换句话说,给定以下内容:.\helloplanet.go.\行星\行星.res.\helloplanet_test.gogotest在Temp\go-build中生成一个exe...但它不会在那里重新创建一个planets\子目录,因此在helloplanet.exe中寻找planets\planetary.res的任何代码当然不会找到该如何应对? 最佳答案 这是一个实

python - 运行 py.test 时出现错误 ImportMismatchError

当我在本地运行测试时,它工作正常,但是在创建docker并在容器内运行后,我遇到了错误。/usr/local/lib/python3.5/site-packages/_pytest/config.py:325:in_getconftestmodulesreturnself._path2confmods[path]EKeyError:local('/apis/db/tests')Duringhandlingoftheaboveexception,anotherexceptionoccurred:/usr/local/lib/python3.5/site-packages/_pytest/