2

在使用 subprocess 执行测试时,如下所示:

subprocess.Popen(["./manage.py", "test", "-x", "--settings=settings_test"], stdout=subprocess.PIPE, stdin=subprocess.PIPE).communicate()

仅捕获测试输出的初始部分。所以我得到了返回的元组

 ("\x1b[32mUsing database: develop\x1b[0m\n\x1b[33mRunning tests against sqlite3 in memory database\x1b[0m\nnosetests --verbosity 1 -x\nCreating test database for alias 'default'...\nDestroying test database for alias 'default'...\n", None)

然而,这错过了关键的最后三行

Ran 260 tests in 70.131s

OK (SKIP=1)

有没有办法使用子进程捕获这个输出?

4

1 回答 1

1

那是因为摘要被写入标准错误,所以你也需要抓住它。

sub = subprocess.Popen(
        ["./manage.py", "test", "-x", "--settings=settings_test"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sub.communicate()
# err should have the summary
于 2013-03-12T17:50:48.963 回答