如何搜索特定的字符串并使用python打印其测试用例ID?

时间:2016-01-26 07:20:25

标签: python regex python-2.7

************************************
     Test Scenario No. 1
     TestcaseID = FB_71125_1
     dpSettingScript = FB_71125_1_DP.txt
************************************
cp /fs/images/nfs/FileRecogTest/MNT/test/Databases/FB_71125_1_device.sqlite $NUANCE_DB_DIR/device.sqlite
    "sync" twice.

Starting the test:

            0#00041511#0000000000# FILERECNTEST: = testScenarioNo (int)1 =
         1900#00046452#0000000000# FILEREIONTESTERROR: expected <FS0000_Pos_Rec_Tone><FS1400_DeviceDisambig_<slot>_ini1>, got <FS0000_Misrec_Tone><FS1000_MainMenu_nm1_004><pause300><FS1000_MainMenu_nm_001>
            0#00046480#0000000000# FILERECNITIONTEST: Preparing test data done
            0#00047026#0000000000# FILERGNITIONTEST: Stopping dialog immediately

     Scenario 1 FAILED.

  The above is the content in my .txt file. There will several .txt file with a content as above. I want to read all those file and check.

1:如果场景失败,那么我必须打印它的TestcaseID。 我能够读取该文件,但我很难检查Scenario是否失败并打印其测试用例ID。 有人可以指导我怎么做吗?

directory =os.path.join("C:\Users\TEST\language")
with open(output_filename, 'w') as f_output:
    for dirpath, dirnames, filenames in os.walk(directory): 
        for filename in filenames:
             if filename.startswith('VCALogParser_output'): 
                cur_file = os.path.join(dirpath, filename)
                f = open(cur_file, "r")
                a = f.read()
                # but how to search for scenario failed and how to print the respective test case id ?
            f.close()

1 个答案:

答案 0 :(得分:0)

因为您可以逐行阅读文件,只需:

  • 在一行中找到TestcaseID,然后存储该ID。
  • 当您找到 Scenario X FAILED 的行时,请打印您已存储的测试ID。

看起来像这样:

with open(cur_file) as test_file:
    test_id = None
    for line in test_file:
        line = line.strip()
        if line.startswith('TestcaseID'):
            test_id = line.partition(' = ')[-1]
        elif line.startswith('[VCALogParser] Scenario'):
            status = line.rpartition(' ')[-1].rstrip('.')
            if status == 'FAILED':
                print(test_id)

我甚至不需要使用正则表达式。您仍然可以,但不需要解析您提供的样本。