草庐IT

test_your_nc

全部标签

unit-testing - 为从中读取的函数填充 os.Stdin

如何在我的测试中填充os.Stdin以获取使用扫描仪从中读取的函数?我使用以下函数通过扫描器请求用户命令行输入:funcuserInput()error{scanner:=bufio.NewScanner(os.Stdin)println("Whatisyourname?")scanner.Scan()username=scanner.Text()/*...*/}现在如何测试这种情况并模拟用户输入?以下示例不起作用。标准输入仍然是空的。funcTestUserInput(t*testing.T){varfile*os.Filefile.Write([]byte("Tom"))os.St

testing - 如何按顺序运行 golang 测试?

当我运行gotest时,我的输出:---FAIL:TestGETSearchSuccess(0.00s)Location:drivers_api_test.go:283Error:Notequal:200(expected)!=204(actual)---FAIL:TestGETCOSearchSuccess(0.00s)Location:drivers_api_test.go:391Error:Notequal:200(expected)!=204(actual)但是在我再次运行gotest之后,我的所有测试都通过了。仅当我重置我的mysql数据库,然后第一次运行gotest时,测试

unit-testing - 如何为另一个包中的方法编写模拟代码

我正在为service.go设置service_test.go。在service.go中,方法是从dao.go中调用的。所以我需要模拟这个dao方法。但我不确定如何编写此模拟方法的代码。这是存储库结构。article├client├api│├main.go│├contoroller││└contoroller.go│├service││└service.go│├dao││└dao.go│├go.mod│├go.sum│└Dockerfile├nginx└docker-compose.yml服务.gofuncGetArticleService(db*sql.DB)[]util.Artic

unit-testing - 调试在命令行中工作,但在 UI 中未命中断点

我在我的Windows10机器上安装了以下内容:VS代码-版本1.38.1Golang-版本go1.13Delve-版本1.3.0我已经在环境变量中设置了GOROOT和GOPATH。我的GOPATH有以下三个文件夹:来源本包装在src下,我创建了一个基本的sam-app。它会自动创建main_test.go文件。当我进行“调试测试”时,UI中没有出现断点。但是,我可以在命令行中使用dlv进行调试。我在launch.json中尝试了不同的配置。他们都没有工作。在我friend的机器上,即使没有配置,UI调试也能正常工作在VSCode设置中-->节点调试-->自动附加-->我已经设置为'o

戈朗 : Testing API using httptest returns 404

我正在尝试测试我编写的与外部API对话的库。我想出了这段代码:import("fmt""net/http""net/http/httptest""net/url""testing")var(//muxistheHTTPrequestmultiplexerusedwiththetestserver.mux*http.ServeMux//clientistheGitHubclientbeingtested.client*Client//serverisatestHTTPserverusedtoprovidemockAPIresponses.server*httptest.Server)fu

testing - 带 channel 的 Golang 测试不退出

下面的Golang测试永远不会退出。我怀疑这与channel死锁有关,但作为一个菜鸟,我不太确定。constuserName="xxxxxxxxxxxx"funcTestSynchroninze(t*testing.T){c,err:=channel.New(github.ChannelName,authToken)iferr!=nil{t.Fatalf("Couldnotcreatechannel:%s",err)return}state:=channel.NewState(nil)ctx:=context.Background()ctx=context.WithValue(ctx,

testing - Golang NewRequest 将 POST 参数传递给 API 进行测试

这是我的测试方法,它创建一个新请求并传递POST参数。url1:="/api/addprospect"data:=url.Values{}data.Add("customer_name","value")b:=bytes.NewBuffer([]byte(data.Encode()))request,err:=http.NewRequest("POST",serverHttp.URL+url1,b)res,err:=http.DefaultClient.Do(request)问题是POSTparam没有被url的函数处理程序获取。你能帮我设置正确的请求吗?谢谢

testing - 针对 xml/字符串测试失败的自定义 testify 输出

我正在使用testify测试XML编码(marshal)处理,并使用strings.Contains检查我希望包含在XML中的行是否确实存在。但是,我想区分实际xml与所需xml。目前,我的代码看起来像这样:func(suite*BookSuite)TestXMLMarshal(){priceXML,priceErr:=xml.Marshal(PriceType{Price:10,Type:"IND"})suite.Nil(priceErr)linePresent:=strings.Contains(string(priceXML),``)iftrue!=linePresent{err

http - 去 Gin 框架 : Testing query and POST with cURL

我正在尝试theREADMEofginframework中的代码示例(“另一个例子:查询+发布表单”):packagemainimport("fmt""github.com/gin-gonic/gin")funcmain(){router:=gin.Default()router.POST("/post",func(c*gin.Context){id:=c.Query("id")page:=c.DefaultQuery("page","0")name:=c.PostForm("name")message:=c.PostForm("message")fmt.Printf("id:%s;p

unit-testing - 如何为测试目的重新定义/模拟 Go 中的方法?

我正在为Go库创建测试。我发现了一个错误:cannotassignto我有以下代码(https://play.golang.org/p/kf0gANb-p-):packagemainimport("bytes""fmt""os/exec""strconv""strings")const(CONSOLE="dialog"KDE="kdialog"GTK="gtkdialog"X="Xdialog"DIALOG_TEST_ENV="test_env"AUTO="auto")const(DIALOG_ERR_CANCEL="exitstatus1"DIALOG_ERR_HELP="exits