【问题标题】:how to search for the specific string and print its test case id using python?如何使用 python 搜索特定字符串并打印其测试用例 id?
【发布时间】:2016-01-26 07:20:25
【问题描述】:
************************************
     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。 我能够读取该文件,但在检查场景是否失败并打印其测试用例 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()

【问题讨论】:

  • 嗯......你已经有一个正则表达式了吗? meta.stackoverflow.com/questions/285733/…
  • edit你的问题,并给出使用你的代码(正则表达式)不起作用,然后在你的问题中解释为什么它不起作用Stack Overflow 不是代码编写服务,因此您需要向我们提供您已经尝试过的内容
  • 我已经解决了这个问题,谢谢。

标签: python regex python-2.7


【解决方案1】:

因为你可以逐行读取文件,很简单:

  • 读取一行中的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)

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

【讨论】:

  • 谢谢,但我收到错误消息:文件“.\framework1.txt”,第 20 行 if filename.startswith('VCALogParser_output'): ^ IndentationError: unindent does not match any external indentation level
  • @ram:复制我的代码,不要混用制表符和空格。将您的编辑器配置为仅使用空格进行缩进。我的答案不包含任何制表符,也不包含缩进错误。我还在那个测试中使用了elif,所以你没有完全复制我的代码。
  • @ram:另外,Python 源代码通常有扩展名.py,而不是.txt
  • 会有几个.txt文件。但我在我的问题中只提到了一个。它应该首先阅读英语,德语,中文等。所以我指定目录并一一读取.txt文件并检查场景是否失败。如果失败,则打印测试用例 ID。
  • 我认为您的代码中缺少 cur_file。我认为,它是:cur_file = os.path.join(dirpath, filename)。我编辑了我的代码。对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 2023-02-08
  • 1970-01-01
  • 2013-05-14
  • 2012-06-21
  • 1970-01-01
  • 2012-11-04
相关资源
最近更新 更多