草庐IT

test_app

全部标签

unit-testing - 仅测试文件中的掩码包名称

为了追求100%的单元测试覆盖率,我们尝试在一个函数中测试多行代码。相关函数调用运行时包://functionNamereturnsastringrepresentingthefunctionnameofthefunctionnstackframesabovethecaller.//ifn=0,thenameofthefunctioncallingfunctionName()willbereturned.funcfunctionName(nint)string{pc,_,_,ok:=runtime.Caller(n+1)if!ok{return"unknownfunction"}me:

unit-testing - 在 Go 中进行单元测试时,是否可以动态断言两个值是否相等?

我刚刚开始使用Go。我正在编写单元测试,我希望能够使用表格进行测试,其中要与实际结果进行比较的结果有时应该或不应该相等。例如,这是我目前拥有的代码:packagemainimport("github.com/stretchr/testify/assert""testing")funcTestFunc(t*testing.T){tables:=[]struct{inputstringcomparisonResultstringshouldBeEqualbool}{{"someirrelevantinput","someresult",true,},{"someotherirrelevan

google-app-engine - https ://onesignal. com/api/v1//notifications : http. DefaultTransport 和 http.DefaultClient 在 App Engine 中不可用

当我尝试使用onesignal环境在golangAppEngine中实现推送通知时。但是我收到错误“http.DefaultTransport和http.DefaultClient在AppEngine中不可用”。这是我的代码,func(c*PushNotificationController)CreateNotification(){client:=onesignal.NewClient(nil)client.AppKey="MyAppKey"client.UserKey="MyUserKey"notifID:=CreateNotifications(client)log.Printl

unit-testing - 如何模拟/单元测试嵌套函数

我有一个函数在其他函数中被调用。send_api.gofunction*send_api*(client*http.Client,urlstring)map[string]string,error{//sendapirequestandparsetheresponseandreturnthedictreturndictmapforeg:{applefruit}}然后这个函数在ma​​in()函数中被调用func*main()*{getmap:=send_api(client*http.Client,"test.com")}good.gofunc*get_dict_key*(keystr

google-app-engine - 在 Google App Engine 中处理 HTTPS 请求

在GAE中我只使用默认域名:https://*.appspot.com,所以我不需要生成自签名证书。GoogleAppEngine文档指定了应如何配置app.yaml来提供SSL连接:https://cloud.google.com/appengine/docs/standard/go/config/appref#handlers_secure但为了在Go中提供HTTPS连接,我编写了以下代码示例,其中我需要指定证书的文件名:import("net/http")funcmain(){gohttp.ListenAndServeTLS(Address,"cert.pem","key.pem

testing - 如何在单元测试中处理新服务器功能

对我来说,单元测试有工作量。所以我使用gotests生成样板测试代码案例。server.gofuncNewServer(cfg*Config,lnet.Listener,driverDriver,dbstore.Store)*Server{s:=&Server{cfg:cfg,listener:l,leader:"",driver:driver,db:db,}s.server=&http.Server{Handler:s.createMux(),}returns}gotests生成server_test.go:funcTestNewServer(t*testing.T){fakeCfg

testing - 在涉及全局变量的不同包中同步测试的最佳方法是什么?

我正在做一个go项目,我的任务是为一个包编写一些测试。测试需要访问不同包中的全局变量。这个全局变量可能在不同包的测试中设置/访问。因为默认情况下,gotesting会针对不同的包并行运行。因此,设置/访问此变量可能会产生竞争条件。最简单的同步方法是创建一个跨越不同包的共享sync.Mutex。话虽如此,我试图将这个sync.Mutex放在声明全局变量的xxx.go的xxx_test.go中,不幸的是,由于作用域,另一个包无法访问这个Mutex局限性。最终,我发现我必须将这个sync.Mutex放到一个专门用于测试的实用程序包中,然后不同的包可以访问这个Mutex以实现该全局变量的同步目

unit-testing - 结构字段还原

这个问题在这里已经有了答案:Howtosetandgetfieldsinstruct'smethod(3个答案)Assignanewvaluetoastructfield(2个答案)关闭5年前。我正在玩Go,但在做一些测试时发现了这种奇怪的情况。我正在使用结构中的方法将变量发送到另一个应该更改字段的方法,但是当我最后检查它时,该字段返回到第一个值,这让我感到困惑。func(thisTVManager)sendMessage(messagestring){fmt.Println("5",this.connector)payload:=map[string]string{"id":"0",

google-app-engine - 无法存储在数据存储 gcloud 中

我正在学习golang和谷歌应用引擎数据存储。我计划调用一个简单的restapi来保存和检索数据存储中的数据。我遵循了一些helloworld教程和官方入门指南。最后我想到的是这个。`packagehelloimport("fmt""log""net/http""encoding/json""cloud.google.com/go/datastore""golang.org/x/net/context""github.com/gorilla/mux""time")typeTaskstruct{Descriptionstring`datastore:"description"`Creat

unit-testing - 如何验证在单独的 go 例程中调用了一个方法

在为一个方法编写单元测试时,我遇到了一个问题。首先是被测代码片段:funcMehodToBeTested(eEntity){gosaveAudit(e)//dosomethingonwhichassertionscanbedone}实体可以被模拟。在saveAudit方法中,调用了Entity.Save方法。在我的UT中,我想断言Entity.Save方法被调用一次。以下是我当前的UT:funcTestMethod(t*testing.T){varmock=&mockEntity{}mock.On("Save").Return(nil)//makecalltofuncundertest