在这里有很多事情在少量的代码。我会尽量保持简洁。Python函数捕获子进程标准输出和标准错误日志文件
我有一个python函数,它运行一个外部程序,并将stdout和stderr都指向一个日志文件。
我使用doctest来测试函数。我需要测试输出捕捉功能。下面的代码显示了我编写函数和测试的尝试。测试失败,没有写入日志文件。我不确定问题出在测试或测试代码中,或者两者兼而有之。建议?
from __future__ import print_function
import subprocess
def run(command_line, log_file):
"""
# Verify stdout and stderr are both written to log file in chronological order
>>> run("echo text to stdout; echo text to stderr 1>&2", "log")
>>> f = open("log"); out = f.read(); f.close()
>>> print(out.strip())
text to stdout
text to stderr
"""
command_line = "set -o pipefail; " + command_line + " 2>&1 | tee " + log_file
# Run command. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError
subprocess.check_call(command_line, shell=True, executable="bash")
测试结果:
$ python -m doctest testclass.py
text to stdout
text to stderr
**********************************************************************
File "testclass.py", line 10, in testclass.run
Failed example:
print(out.strip())
Expected:
text to stdout
text to stderr
Got:
<BLANKLINE>
**********************************************************************
1 items had failures:
1 of 3 in testclass.run
***Test Failed*** 1 failures.
你尝试,从产生的bash命令'“set -o pipefail;”+ command_line +“2>&1 | tee” + log_file'在终端?输出是什么样的? –
发送到终端的文本与此命令一样:set -o pipefail;将文本回显到stdout;将文本回显到stderr 1>&2 2>&1 | tee log – Steve
@ Jean-FrançoisFabre这是大批量生产过程中的一小部分,用于生成用户检查处理结果所需的大型日志文件。 – Steve