dongmeiyi2266 2019-03-15 05:59
浏览 114
已采纳

流程可以读取自己的“标准输出”流吗?

How would a process read its own output stream? I am writing automated tests which start a few application sub-processes (applications) in the same process as the test. Therefore, the standard out is a mix of test output and application output.

I want to read the output stream at runtime and fail the test if I see errors from the application. Is this possible/feasible? If so, how do I do it?

Note: I know I could start the applications as their own separate processes and then read their output streams. That's a lot of work from where I am now.

Also note, this is not a dupe of How to test a function's output (stdout/stderr) in Go unit tests, although that ticket is similar and helpful. The other ticket is about capturing output for a single function call. This ticket is about reading the entire stream, continuously. The correct answer is also a little different - it requires a pipe.

  • 写回答

2条回答 默认 最新

  • dsztc99732 2019-03-15 06:12
    关注

    Yes, You may use os.Pipe() then process it yourself:

    tmp := os.Stdout
    r, w, err := os.Pipe()
    if err != nil {
        panic(err)
    }
    os.Stdout = w
    

    Or divert os.Stdout to a another file or strings.Builder.
    Here is the detailed answer:
    In Go, how do I capture stdout of a function into a string?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了