使用Python读取XMind格式测试用例,循环处理字符串

  1. 首先贴上使用Xmind制作的测试用例格式范本
    在这里插入图片描述
  2. 读取Xmind文件的代码为
ReadXmind = xmind_to_dict(FileName)
  1. 在读取Xmind编写的测试用例时,发现Python读取出来的字符串结构较复杂,因此特地将读取出来的字符串重新赋值进行测试,循环拿到最后的预置条件,测试步骤,实际结果
import jsonpath
ReadXmind = [{'title': 'sheet', 'topic': {'title': '文件名', 'topics':
    [{'title': 'sheet名1', 'topics':
        [{'title': '模块1', 'topics': 
            [{'title': '功能1', 'topics': 
                [{'title': '测试点1', 'topics': 
                    [{'title': '预置条件1', 'topics': 
                        [{'title': '测试步骤1', 'topics': 
                            [{'title': '预期结果1'}]}]}]}, 
                 {'title': '测试点2', 'topics': 
                     [{'title': '预置条件2', 'topics': 
                         [{'title': '测试步骤2', 'topics': 
                             [{'title': '预期结果2'}]}]}, 
                      {'title': '预置条件3', 'topics': 
                          [{'title': '测试步骤3', 'topics': 
                              [{'title': '预期结果3'}]}]}]}]}, 
             {'title': '功能2', 'topics': 
                 [{'title': '测试点3', 'topics': 
                     [{'title': '预置条件4', 'topics': 
                         [{'title': '测试步骤4', 'topics': 
                             [{'title': '预期结果4'}]}]}]}, 
                  {'title': '测试点4', 'topics': 
                      [{'title': '预置条件5', 'topics': 
                          [{'title': '测试步骤5', 'topics': 
                              [{'title': '预期结果5'}]}]}, 
                       {'title': '预置条件6', 'topics': 
                           [{'title': '测试步骤6', 'topics': 
                               [{'title': '预期结果6'}]}]}]}]}]}, 
         {'title': '模块2', 'topics': 
             [{'title': '功能3', 'topics': 
                 [{'title': '测试点5', 'topics': 
                     [{'title': '预置条件7', 'topics': 
                         [{'title': '测试步骤7', 'topics': 
                             [{'title': '预期结果7'}]}]}]}, 
                  {'title': '测试点6', 'topics': 
                      [{'title': '预置条件8', 'topics': 
                          [{'title': '测试步骤8', 'topics': 
                              [{'title': '预期结果8'}]}]}, 
                       {'title': '预置条件9', 'topics': 
                           [{'title': '测试步骤9', 'topics': 
                               [{'title': '预期结果9'}]}]}]}]}, 
              {'title': '功能4', 'topics': 
                  [{'title': '测试点7', 'topics': 
                      [{'title': '预置条件10', 'topics': 
                          [{'title': '测试步骤10', 'topics': 
                              [{'title': '预期结果10'}]}]}]}, 
                   {'title': '测试点8', 'topics': 
                       [{'title': '预置条件11', 'topics': 
                           [{'title': '测试步骤11', 'topics': 
                               [{'title': '预期结果11'}]}]}, 
                        {'title': '预置条件12', 'topics': 
                            [{'title': '测试步骤12', 'topics':
                                [{'title': '预期结果12'}]}]}]}]}]}]}, 
     {'title': 'sheet名2', 'topics': 
         [{'title': '模块3', 'topics': 
             [{'title': '功能5', 'topics': 
                 [{'title': '测试点9', 'topics': 
                     [{'title': '预置条件13', 'topics': 
                         [{'title': '测试步骤13', 'topics': 
                             [{'title': '预期结果13'}]}]}]}, 
                  {'title': '测试点10', 'topics': 
                      [{'title': '预置条件14', 'topics': 
                          [{'title': '测试步骤14', 'topics': 
                              [{'title': '预期结果14'}]}]}, 
                       {'title': '预置条件15', 'topics': 
                           [{'title': '测试步骤15', 'topics': 
                               [{'title': '预期结果15'}]}]}]}]}, 
              {'title': '功能6', 'topics': 
                  [{'title': '测试点11', 'topics': 
                      [{'title': '预置条件16', 'topics': 
                          [{'title': '测试步骤16', 'topics': 
                              [{'title': '预期结果16'}]}]}]}, 
                   {'title': '测试点12', 'topics': 
                       [{'title': '预置条件17', 'topics': 
                           [{'title': '测试步骤17', 'topics': 
                               [{'title': '预期结果17'}]}]}, 
                        {'title': '预置条件18', 'topics': 
                            [{'title': '测试步骤18', 'topics': 
                                [{'title': '预期结果18'}]}]}]}]}]}, 
          {'title': '模块4', 'topics': 
              [{'title': '功能7', 'topics': 
                  [{'title': '测试点13', 'topics': 
                      [{'title': '预置条件19', 'topics': 
                          [{'title': '测试步骤19', 'topics': 
                              [{'title': '预期结果19'}]}]}]}, 
                   {'title': '测试点14', 'topics': 
                       [{'title': '预置条件20', 'topics': 
                           [{'title': '测试步骤20', 'topics': 
                               [{'title': '预期结果20'}]}]}, 
                        {'title': '预置条件21', 'topics': 
                            [{'title': '测试步骤21', 'topics': 
                                [{'title': '预期结果21'}]}]}]}]}, 
               {'title': '功能8', 'topics': 
                   [{'title': '测试点15', 'topics': 
                       [{'title': '预置条件22', 'topics': 
                           [{'title': '测试步骤22', 'topics': 
                               [{'title': '预期结果22'}]}]}]}, 
                    {'title': '测试点16', 'topics': 
                        [{'title': '预置条件23', 'topics': 
                            [{'title': '测试步骤23', 'topics': 
                                [{'title': '预期结果23'}]}]}, 
                         {'title': '预置条件24', 'topics': 
                             [{'title': '测试步骤24', 'topics': 
                                 [{'title': '预期结果24'}]}]}]}]}]}]}]}, 
              'structure': 'org.xmind.ui.logic.right'}]
  1. 循环获取字符串的代码为下面,最后可以得到最终循环的预置条件,测试步骤,预期结果等内容

FileName = jsonpath.jsonpath(ReadXmind[0],'$.topic.title')[0]
#print('FileName =',FileName)
FileNameList = jsonpath.jsonpath(ReadXmind[0],'$.topic.topics')[0]
#print(len(FileNameList),FileNameList)
for a in range(0,len(FileNameList)):
    SheetName = jsonpath.jsonpath(FileNameList[a],'$.title')[0]
    #print("SheetName =",SheetName)
    SheetNameList = jsonpath.jsonpath(FileNameList[a], '$.topics')[0]
    #print(len(SheetNameList), "SheetNameList =", SheetNameList[a])

    for b in range(0,len(SheetNameList)):
        ModuleList = SheetNameList[b]
        #print("ModuleList =",ModuleList)
        ModuleName = jsonpath.jsonpath(ModuleList,'$.title')[0]
        #print('ModuleName =',ModuleName)
        FunctionList = jsonpath.jsonpath(ModuleList,'$.topics')[0]
        #print('FunctionList =',FunctionList)
        for c in range(0,len(FunctionList)):
            TmpFunctionlist = FunctionList[c]
            FunctionName = jsonpath.jsonpath(TmpFunctionlist,'$.title')[0]
            TestPointList = jsonpath.jsonpath(TmpFunctionlist,'$.topics')[0]
            #print("FunctionName = %s,TestPoint = %s"%(FunctionName,TestPointList))
            for d in range(0,len(TestPointList)):
                TmpTestPoint = TestPointList[d]
                TestPointName = jsonpath.jsonpath(TmpTestPoint,'$.title')[0]
                TestList = jsonpath.jsonpath(TmpTestPoint,'$.topics')[0]
                #print("TestPointName = %s,TestList = %s"%(TestPointName,TestList) )
                for e in range(0,len(TestList)):
                    TmpTestList = TestList[e]
                    #print(TmpTestList)
                    PresetConditions = jsonpath.jsonpath(TmpTestList,'$.title')[0]
                    TestStep = jsonpath.jsonpath(TmpTestList,'$.topics[0].title')[0]
                    ExpectedResults = jsonpath.jsonpath(TmpTestList,'$.topics[0].topics[0].title')[0]
                    print('PresetConditions = %s,TestStep = %s,ExpectedResults = %s'%(PresetConditions,TestStep,ExpectedResults))


在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西门一刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值