草庐IT

MaCAPI测试工具

全部标签

unit-testing - 如何测试在 golang 中打印到控制台的方法?

我有下一个结构。packageloggerimport"fmt"typeIPrinterinterface{Print(valuestring)}typeConsolePrinterstruct{}func(cp*ConsolePrinter)Print(valuestring){fmt.Printf("thisisvalue:%s",value)}测试范围说我需要测试ConsolePrinterPrint方法。如何覆盖这个方法?谢谢。 最佳答案 根据@icza写的评论,我在下面编写了测试。funcTestPrint(t*testi

go - 未能使测试套件通过 golang "sayHello() used as value"

我试图让这个测试套件通过命令提示符(hello-world_test.go):packagehelloworldimport"testing"funcTestHelloWorld(t*testing.T){output:=sayHello()if"Hello,World!"!=output{t.Fatalf("output:%s\nexpected:Hello,World!",output)}}我的代码如下(helloworld.go):packagehelloworldimport"fmt"funcsayHello(){fmt.Println("Hello,World!")}通过命令

go - 如何在 Hyperledger Fabric 代码上运行测试?

很抱歉打扰你们,但我想知道如何才能最初运行HyperledgerFabric代码。我下载了所有Go、Docker、Dockercomposer、VSCode和这些Go插件。当我在/orderer/solo中使用gotestconsensus_test.go时。它会给我这些答案,那么我该如何运行这些代码呢?谢谢。$fabric@ubuntu:~/go/src/github.com/hyperledger/fabric/orderer/consensus/solo$gotestconsensus_test.go#command-line-arguments./consensus_test.

go - 在 Go 工具中忽略命令行参数

我目前正在用Go编写我的第一个小工具,我遇到了一个问题,无论我做什么,一个命令行参数都会被忽略。mainFile:=flag.String("input","./generator.ini","theinputfile")outputFile:=flag.String("foo","Defaultdirectoryfoobarblablabla","theoutputdirectory")fmt.Println("Param:",*outputFile)outputDir:=filepath.Clean(*outputFile)flag.Parse()fmt.Println("Outp

go - 如何在 Golang 的测试中引用字符串中的变量?

以下在测试中有效:ifactualKey!=expectedKey{t.Fatalf("Failed.Actual:%q.Expected:%q",actualKey,expectedKey)}在主要代码中:m["Keyword"+kw+"foundon"+url]=0,但这失败了:m["Keyword%qfoundon%q",kw,url]=0正如@JimB建议的那样fmt.Sprintf可用于。以下作品:msg:=fmt.Sprintf("Keyword%qfoundon%q",kw,url)m[msg]=0问题将此方法称为变量引用是否正确?如果为假,它叫什么?这是最简洁的实现方式

Golang 无法在同一个包中进行测试

在为我的一个源文件(commonutil.go)创建单元测试时遇到问题packageutilimport"github.com/nu7hatch/gouuid"//GenerateUUIDReturnsgeneratedUUIDsequencefuncGenerateUniqueID(hostnamestring)(string,error){varresult,err=uuid.NewV4()returnhostname+":"+result.String(),err}对于上面的源,我创建了测试文件“commonutil_test.go”(在同一个包中)packageutilimpo

go - 接口(interface)上的 nil 测试失败

这个问题在这里已经有了答案:Hidingnilvalues,understandingwhyGofailshere(3个答案)关闭6年前。谁能给我解释一下这段代码?调用它时,err!=nil返回true,从而导致nil指针引用。typeEstruct{}func(eE)Error()string{return""}funcDoStuff()*E{returnnil}funcmain(){varerr*Eerr=DoStuff()log.Println(err)//niltestErr(err)}functestErr(errerror){iferr!=nil{log.Println("

go - 为什么 runtime.caller(0) 在单元测试中使用 '--cover' 参数给出不同的路径

当尝试不带任何参数执行单元测试函数时,它给出以下输出路径C:/ProgramFiles(x86)/Go1.8\bin\go.exe"test-vgclassec/skyline/loggers-run^TestCreateLogFilesPath$filePath:===C:/GerritNew/goclassec/src/gclassec/skyline/loggers/logs.go但是当我尝试使用任何参数运行它时,例如--cover文件路径值更改如下:"C:/ProgramFiles(x86)/Go1.8\bin\go.exe"test-v--covergclassec/skyl

unit-testing - golang 这应该是一个集成测试吗?

我有一个实现数据库接口(interface)的方法,该方法将一个“对象”插入到数据库中。typedatabaseinterface{createLog(logDoc)(bool,error)}typemongostruct{databasestringcollectionstring}func(mmongo)createLog(llogDoc)(bool,error){s,err:=mgo.Dial("mongo")defers.Close()iferr!=nil{returnfalse,err}err=s.DB(m.database).C(m.collection).Insert(l

go - 单元测试不覆盖本地导入包

我是golang的新手,想了解如何让这个场景发挥作用?这是我的结构GOPATH设置为/Users/xyz/project/Users/xyz/project/src/main.go//importcalculatorandcallSUMwithtwointegeres/Users/xyz/project/src/main_test.go//testfunction/Users/xyz/project/src/calculator/sum.go//SUMfunction(addtwointegers)我有一个主要的go文件,它导入“计算器”,这是一个本地包。当我运行时gotest-cov