草庐IT

test_dict

全部标签

testing - 测试中的静态文件

当我在Go中编写需要静态文件的测试时(例如文件hello.txt我测试我的程序hello.txt被正确读取),其中我应该放置静态文件吗?我应该如何在测试文件中解决它们?也就是说,目前我的设置是一个本地目录,GOPATH设置为这个目录。我有src/mypkg/myfile.gomyfile_test.gotestfiles/hello.txtworld.txt现在在myfile_test.go中,我不想使用绝对路径来引用testfiles/hello.txt。有什么惯用的方法可以做到这一点吗?这是一个合理的布局吗? 最佳答案 常用的做

testing - 测试中的静态文件

当我在Go中编写需要静态文件的测试时(例如文件hello.txt我测试我的程序hello.txt被正确读取),其中我应该放置静态文件吗?我应该如何在测试文件中解决它们?也就是说,目前我的设置是一个本地目录,GOPATH设置为这个目录。我有src/mypkg/myfile.gomyfile_test.gotestfiles/hello.txtworld.txt现在在myfile_test.go中,我不想使用绝对路径来引用testfiles/hello.txt。有什么惯用的方法可以做到这一点吗?这是一个合理的布局吗? 最佳答案 常用的做

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

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

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

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-如何在进行

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-如何在进行