草庐IT

rect-based-test

全部标签

testing - 你如何测试 golang 命令行输出

这个问题在这里已经有了答案:Howtotestafunction'soutput(stdout/stderr)inunittests(3个答案)关闭3年前。我想测试golang命令行应用程序的输出,但我不太确定如何使用go的测试库来实现。假设我有这样一个程序:packagemainimport("flag""fmt")funcmain(){const(cityDefault="SanFrancisco"cityDoc="thecityyouwanttheforecastfor")varcitystringflag.StringVar(&city,"city",cityDefault,c

go - testing.M 在 golang 中恢复

此恢复有效:funcTestSomeTest(t*testing.T){deferfunc(){r:=recover()fmt.Println("recovery")fmt.Println(r)}()panic("panichere")}但这不是:funcTestSomeTest(t*testing.T){panic("panichere")}funcTestMain(m*testing.M){deferfunc(){r:=recover()fmt.Println("recovery")fmt.Println(r)}()ret:=m.Run()os.Exit(ret)}为什么?我希望p

go - testing.M 在 golang 中恢复

此恢复有效:funcTestSomeTest(t*testing.T){deferfunc(){r:=recover()fmt.Println("recovery")fmt.Println(r)}()panic("panichere")}但这不是:funcTestSomeTest(t*testing.T){panic("panichere")}funcTestMain(m*testing.M){deferfunc(){r:=recover()fmt.Println("recovery")fmt.Println(r)}()ret:=m.Run()os.Exit(ret)}为什么?我希望p

go - 将 SHA1 十六进制转换为 Base 16 整数

我需要一些帮助将算法从Ruby移植到Go。在Ruby中我有:hex=Digest::SHA1.hexdigest(str).to_i(16)hex.to_s(32)创建一个SHA1十六进制字符串,将其转换为16进制整数,然后再转换回32进制字符串。我如何在Go中实现同样的目标? 最佳答案 这是一个示例代码(Playground:https://play.golang.org/p/izBIq97-0S):packagemainimport("crypto/sha1""encoding/base32""fmt""strings")fun

go - 将 SHA1 十六进制转换为 Base 16 整数

我需要一些帮助将算法从Ruby移植到Go。在Ruby中我有:hex=Digest::SHA1.hexdigest(str).to_i(16)hex.to_s(32)创建一个SHA1十六进制字符串,将其转换为16进制整数,然后再转换回32进制字符串。我如何在Go中实现同样的目标? 最佳答案 这是一个示例代码(Playground:https://play.golang.org/p/izBIq97-0S):packagemainimport("crypto/sha1""encoding/base32""fmt""strings")fun

unit-testing - 在golang中重新定义const进行测试

我正在为服务和测试编写一个http客户端,我想使用net/http/httptest服务器而不是调用远程API。如果我将baseUrl设置为我的测试服务器的url的全局变量,我可以轻松地做到这一点。但是,这会使生产代码更加脆弱,因为baseUrl也可以在运行时更改。我的偏好是使baseUrl成为生产代码的const但仍然可以更改。packagemainconstbaseUrl="http://google.com"//inmain_test.gots:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*htt

unit-testing - 在golang中重新定义const进行测试

我正在为服务和测试编写一个http客户端,我想使用net/http/httptest服务器而不是调用远程API。如果我将baseUrl设置为我的测试服务器的url的全局变量,我可以轻松地做到这一点。但是,这会使生产代码更加脆弱,因为baseUrl也可以在运行时更改。我的偏好是使baseUrl成为生产代码的const但仍然可以更改。packagemainconstbaseUrl="http://google.com"//inmain_test.gots:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*htt

arrays - 为什么在使用 base64 编码字节数组时会出现 "index out of range"错误?

将字节数组编码为base64字节数组时,以下代码会产生运行时indexoutofrange错误。如何解决?packagemainimport("fmt""encoding/base64")funcmain(){data:=[]byte("stringofdata")varencodedData[]bytebase64.StdEncoding.Encode(encodedData,data)fmt.Println(encodedData)}Playgroundhere 最佳答案 错误是:panic:runtimeerror:index

arrays - 为什么在使用 base64 编码字节数组时会出现 "index out of range"错误?

将字节数组编码为base64字节数组时,以下代码会产生运行时indexoutofrange错误。如何解决?packagemainimport("fmt""encoding/base64")funcmain(){data:=[]byte("stringofdata")varencodedData[]bytebase64.StdEncoding.Encode(encodedData,data)fmt.Println(encodedData)}Playgroundhere 最佳答案 错误是:panic:runtimeerror:index

testing - 如何在进行测试时抑制 [no test files] 消息

我正在项目的根目录中运行gotest./...但有几个包没有任何测试并报告[notestfiles].如果我运行gotest./...|grep-v'notestfiles'如果测试失败,我会丢失gotest的返回代码。我可以忽略没有测试的包,同时从项目的根开始递归测试所有内容吗? 最佳答案 是这样的吗?mkfifo/tmp/fifo-$$grep-v'notestfiles'/tmp/fifo-$$RES=$?rm/tmp/fifo-$$exit$RES 关于testing-如何在进行