调试golang测试

调试某个go test程序的时候,需要实现confirm/approve功能: 测试完testCase1之后(或者说是某个断点),用户输入yes,执行下一个case,输入no, 则退出整个测试。 分析 下意识的觉得这个很好实现,调用fmt.Scan应该就OK了。 但是写的时候才发现,go test 会强制重定向os.Stdin = /dev/null忽略所有的 stdin输入, 所以没法使用 fmt.Scan来等待键盘(用户)的输入: 1 2 3 4 5 // fmt.Scan reads from os.Stdin, which is set to /dev/null when testing. // Tests should be automated and self-contained anyway, so you don't want // to read from stdin. // Either use an external file, a bytes.Buffer, or a strings.Reader and // call scan.Fscan if you want to test **the literal** `scan` Function. 解决方案 可以参考事件驱动,使用named pipes来实现confirm/approve功能: ...

October 14, 2020 · datewu