当我使用./program.py> temp.out
运行我的程序时我首先获得所有单元测试输出,然后是我在python中输入的打印件。无论如何,我可以让它以它在屏幕上的方式显示在此文件中吗?
Test Results Suite "curl"
Name: Checks Failures Time (s)
couch check: - - Disabled
couch check fail: - - Disabled
database check fail: - - Disabled
create database: - - Disabled
database check: - - Disabled
upload design doc: - - Disabled
remove_witness: - - Disabled
====================================================================
Total: 0 0
Passed
+=================================================================+
| Running: hba_test |
| Skipping:abort/"Basic Sanity" delayedabort/"Abort Control List" |
+=================================================================+
+====================+
| Skipping: sdt_test |
+====================+
+======================+
| Skipping: dtd_tester |
+======================+
+===============+
| Running: pssm |
+===============+
+==============+
| Running: psm |
+==============+
这是执行每个单元测试并在其周围打印单独标题的代码
#calculate lengths to make sure header is correct length
l1 = len(x)
l2 = len(y)
#if entire test suite is to be disabled
if disable:
headerBreak ="+" + "="*(l1+12) + "+"
print headerBreak
print "| Skipping: %s |" % x
#if the test suite will be executed
else:
headerBreak = "+" + "="*(max(l1,l2)+11) + "+"
print headerBreak
print "| Running: %s" % x, ' '*(l2-l1)+ '|'
#if some suites are disabled but some are still running
if 'disable=' in test:
print "| Skipping:%s |" % y
print headerBreak
#bitshift right to obtain correct return value, execution of each test.
returnValue = os.system(path) >> 8
#running total of failures in the program.
failures += returnValue
运行方法的最后一段代码
#execute tests failures = execTests(path, testList)
#exit program with returncode as number of failures sys.exit(failures)
应该是这样的:
+==============+
| Running: ssm |
+==============+
Test Results Suite "Secondary Set Manager Tests"
Name: Checks Failures Time (s)
SSM_1 validate checks: 30 0 0.002
SSM_2 group create: 6 0 0.001
SSM_3 rcvd invalid group: 3 0 0.001
SSM_4 rcvd invalid data: 9 0 0.001
SSM_5 aborted subset: 7 0 0.000
SSM_6 pri node down abort: 14 0 0.000
SSM_7 excess ios in subsets: 6 0 0.000
SSM_8 all ss received: 11 0 0.000
SSM_9 applying ss: 12 0 0.000
SSM_10 applying ss 2: 18 0 0.000
SSM_11 subsets complete: 32 0 0.001
SSM_12 subsets complete errors: 19 0 0.000
SSM_13 apply waiting set: 40 0 0.000
SSM_14 extend test: 14 0 0.000
SSM_15 group destroy: 6 0 0.000
SSM_16 null params: 2 0 0.001
SSM_17 stop group: 26 0 0.001
SSM_18 dupe receive: 6 0 0.000
SSM_19 apply waiting set: 36 0 0.001
====================================================================
Total: 297 0
Test Results Suite "Secondary Subset Manager Tests"
Name: Checks Failures Time (s)
SSSM_1 Validate Checks: 14 0 0.001
SSSM_2 Steady State: 92 42 0.001
SSSM_3 Multi Sequence: 417 227 0.003
SSSM_4 Test Abort: 69 6 0.001
SSSM_5 Test Inconsistent Mreq: 10 0 0.001
SSSM_6 Test extend: 37 1 0.001
SSSM_7 Test unexpected IO: 11 0 0.001
SSSM_8 test null params: 5 0 0.000
SSSM_9 exceptions: 0 1 0.001
SSSM_10 done_incomplete: 20 0 0.001
SSSM_11 failed io: 92 42 0.001
====================================================================
Total: 767 319
Failed Cases [ssm]:
Secondary Subset Manager Tests/"SSSM_2 Steady State"
Secondary Subset Manager Tests/"SSSM_3 Multi Sequence"
Secondary Subset Manager Tests/"SSSM_4 Test Abort"
Secondary Subset Manager Tests/"SSSM_6 Test extend"
Secondary Subset Manager Tests/"SSSM_9 exceptions"
Secondary Subset Manager Tests/"SSSM_11 failed io"
Overall Failures: 319
答案 0 :(得分:3)
当您将输出从终端重定向到文件时,缓冲模式会从缓冲行更改为使用固定大小的缓冲区。这意味着换行符不再触发刷新。
因此,您的print
输出正在缓冲,但正在运行os.system()
的测试在进程完成时刷新了他们的缓冲区。
解决方案是在运行print
之前,在os.system()
语句后明确刷新:
import sys
# ....
print headerBreak
sys.stdout.flush()
returnValue = os.system(path) >> 8