草庐IT

main_test

全部标签

unit-testing - 如何在我的 go 测试中实现 faker?

我正在尝试学习Go并编写我的第一个测试。我想弄清楚如何实现一个伪造者来测试terraform上的CRUD。我已经弄清楚如何导入faker并创建要使用的变量。我尝试使用fmt.Println来查看它是否生成,但是如果它是一个测试,我在哪里可以看到Println。当我去测试时它不打印。functestCheckTritonUserDelete(s*terraform.State)error{fmt.Println("INsidetestCheckTritonUserDelete")fmt.Println(fake.UserName())returnnil//这是我要打印的函数

unit-testing - 在单元测试中使用随机数与硬编码值

这个问题在这里已经有了答案:WhatarethedownsidesusingrandomvaluesinUnitTesting?(11个答案)RandomdatainUnitTests?(20个答案)关闭3年前。当我写测试的时候,我喜欢用随机数来计算东西。例如funcinit(){rand.Seed(time.Now().UnixNano())}funcTestXYZ(t*testing.T){amount:=rand.Intn(100)cnt:=1+rand.Intn(10)fori:=0;i当然有缺点expected:=calcExpected(amount,cnt)因为需要根据随

go - 开始时遇到困难。 `package main` 引发运行时错误 - 索引超出范围?

我是围棋的初学者。而我刚刚安装了gophernotes,打算主要使用JupyterNotebook进行编程。此程序在Jupyter中运行时出现以下错误:Cell1:packagemainOut1:runtimeerror:indexoutofrangeCell2:import"fmt"funcmain(){fmt.Println("helloworld")}main()Out2:helloworld当我在test.go中编写相同的内容并从bash执行时:goruntest.go,我得到以下信息:Deepaks-MacBook-Air:JUPYTERdeepak$goruntest.go

go - 如何在main.go中找到run函数的入口?

我不是GO程序员,当我阅读GO的代码时,我发现了这样的代码funcmain(){......run(options)}我很困惑函数运行将运行什么?谁能帮忙? 最佳答案 好吧,公平地说,您发布的代码将产生以下内容:prog.go:4:3:syntaxerror:unexpected...,expecting}https://play.golang.org/p/HMv-FydjKWf然而,在一个更完整的例子中:packagemainimport"fmt"typeOptionsstruct{Enabledbool}funcrun(opts

testing - 我应该使用哪种方法来测试 golang 中的 func main()?

我在main.go中有一个函数main()可以完成这项工作,所有其他函数都在它下面(我没有在这里包括它们)。因此,当我为main中包含的所有funcs编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖main函数中的代码。我知道测试库中有一个TestMain函数可以完成这项工作,但我就是不知道如何让它工作,以便测试涵盖funcmain()。下面是我的main()函数,它没有被测试覆盖...funcmain(){c,err:=getConfig()iferr!=nil{log.Fatal(err)}slideshows,err:=getSlideshows(c)ifer

Goroutine 从 main 返回后没有执行完。为什么?

我正在尝试理解golang中的上下文。我从https://golang.org/pkg/context/#example_WithCancel复制了一个例子并稍微改变一下:Playground:https://play.golang.org/p/Aczc2CqcVZRpackagemainimport("context""fmt""time")funcmain(){//gengeneratesintegersinaseparategoroutineand//sendsthemtothereturnedchannel.//Thecallersofgenneedtocancelthecon

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()函数以返回您

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-去单元测试

unit-testing - 错误 : suite. go:61: test paniced: reflect: Call with too few input arguments

我正在golang中设置单元测试。但是现在我在运行gotest-v时遇到错误。我想解决这个错误并使测试成功。article├client├api│├main.go│├contoroller││├contoroller.go││└contoroller_test.go│├service││├service.go││└service_test.go│├dao││├dao.go││└dao_test.go│├s3││├s3.go││└s3_test.go│├go.mod│├go.sum│└Dockerfile├nginx└docker-compose.yml现在我正在为service.go设

web - 为什么当我再次运行 main.go 时 View 相同

主.gopackagemainimport("html/template""net/http")vartemplates=template.Must(template.ParseGlob("./templates/*"))funcviewHandler(whttp.ResponseWriter,r*http.Request){err:=templates.ExecuteTemplate(w,"indexPage",nil)iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}}funcmain