Python覆盖到错误ord中的文件

2024-05-03 16:02:20 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行我的程序时/程序.py&燃气轮机;输出温度你知道吗

我首先得到所有的单元测试输出,然后在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

Tags: testifmanagertestsrunningssmprintsubset
1条回答
网友
1楼 · 发布于 2024-05-03 16:02:20

当您将输出从终端重定向到文件时,缓冲模式从行缓冲变为使用固定大小的缓冲。这意味着换行不再触发刷新。你知道吗

因此,您的print输出将被缓冲,但是您使用os.system()运行的测试将在进程完成时刷新它们的缓冲区。你知道吗

解决方案是在运行print语句之前显式刷新os.system()

import sys

# ....
print headerBreak
sys.stdout.flush()
returnValue = os.system(path) >> 8

相关问题 更多 >