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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法