美文网首页Python
python 读取文本实践:读取文件中的url及描述,拼接成字典

python 读取文本实践:读取文件中的url及描述,拼接成字典

作者: 十一岁的加重 | 来源:发表于2018-05-29 17:05 被阅读4次

    场景:需要做个初步的接口自动化测试,所以需要对每个接口进行命名的url拼接
    接口命名需要有中文注释了url
    这个中文注释呢,在Xcode的pch里,且格式不太一样,看了下weex的url的文件,注释还比较统一

    /**
     * 这里是这个接口的中文描述
     */
    exports.someString = "someString"
    

    目标是生成这样的格式的

    {"这里是这个接口的中文描述 someString" : "someString" }
    

    初步写了个简单的脚本getApisFromWeexJSFile.py,目前可以 实现,有问题再逐步完善

    # coding=utf-8
    
    import json
    import sys
    import linecache
    import json
    import re
    reload(sys)
    # sys.setdefaultencoding('utf8')
    
    urlStringJsFilePath = '/Users/mac/Desktop/companyProj/weex-qylc/src/include/urlconstant.js'
    urls = []
    file_object = open(urlStringJsFilePath, 'rU')
    try: 
        for lineIdx,line in enumerate(file_object):
            #判断是否是空行或注释行
            if not len(line) or line.startswith('#') or line.startswith('/*') or line.startswith(' *') or line.startswith('>>>'):  
                continue
            elif line.startswith('exports.'):
                # 去除/t空格之类的
                rightLine = "".join(line.split())
                lineStrings = rightLine.split('=');
                if len(lineStrings) > 1:
                    sub1Str = lineStrings[1]
                    sub2Str = sub1Str.replace("\"", "")
                    sub3Str = sub2Str.replace('\";', "")
                    sub4Str = sub3Str.replace('\'', "")
                    sub5Str = sub4Str.replace(';', "")
                    urlString = sub5Str.replace('\';', "")
                    findIdx1 = lineIdx -1
                    findStr2 = linecache.getline(urlStringJsFilePath,findIdx1)
                    urlKeyString = "".join(findStr2.split())
                    urlKey = urlKeyString.replace('*','')+ ' ' + urlString
                    urls.append({urlKey : urlString})
    
                    
        print json.dumps(urls, ensure_ascii=False, encoding='UTF-8')
    finally:
         file_object.close()
    
    

    针对Xcode里的pch文件又写了个getApisFromXcodePchFile

    
    # coding=utf-8
    
    import json
    import sys
    import linecache
    import json
    import re
    reload(sys)
    # sys.setdefaultencoding('utf8')
    
    urlStringJsFilePath = '你们项目的pch位置'
    urls = []
    file_object = open(urlStringJsFilePath, 'rU')
    try: 
        for lineIdx,line in enumerate(file_object):
            
            isNotBaseURLStringLine = \
            line.count('requestIP') > 0 or \
            line.count('H5BaseIP') > 0 or \
            line.count('swiftidIP') > 0 or \
            line.count('URL_TRACE_MONITOR') > 0 or \
            line.count('codeNum') > 0 or \
            line.count('WeexBase') > 0 or \
            line.count('dynamicMaint') > 0 or \
            line.count('gatewayRequestUrl') > 0 or \
            line.count('dcRequestUrl') > 0 or \
            line.count('baseMessage_MONITOR')>0 or\
            line.count('CFBundleDisplayName') > 0 or\
            line.count('CFBundleShortVersionString') > 0 or\
            line.count('CFBundleVersion') > 0 or\
            line.count('CFBundleDisplayName') > 0 or\
            line.count('WJExceptionHandler_crash') > 0 or\
            line.count('__FUNCTION__') > 0 or\
            line.count('__FILE__') > 0 or \
            line.count('#pragma mark') > 0 or \
            line.count('/') <= 0
        
    
            #判断是否是空行或注释行
            if not len(line)  or line.startswith('/*') or line.startswith(' *') or line.startswith('>>>'):  
                continue
            elif (line.startswith('#define ') or line.startswith('static')) and not isNotBaseURLStringLine :
                # 去除/t空格之类的
                rightLine = "".join(line.split())
                lineStrings = rightLine.split('@');
                if len(lineStrings) > 1:
                    sub1Str = lineStrings[1]
                    sub2Str = sub1Str.replace("\"", "")
                    sub3Str = sub2Str.replace('\";', "")
                    sub4Str = sub3Str.replace('\'', "")
                    sub5Str = sub4Str.replace(';', "")
                    urlString = sub5Str.replace('\';', "")
                    findIdx1 = lineIdx -1
    
                    findStr2 = linecache.getline(urlStringJsFilePath,lineIdx)
                    if findStr2.strip()=="" or findStr2.count('#define') >0:
                        firstStr = rightLine.split('@')[0];
                        findStr2 = firstStr.replace('#define', '')
                    urlKeyString = "".join(findStr2.split())
                    urlKeyString2 = urlKeyString.replace('/**','')
                    urlKeyString21 = urlKeyString2.replace('/*','')
                    urlKeyString212 = urlKeyString21.replace('-','')
                    urlKeyString3 = urlKeyString212.replace('*/','')
                    urlKey = urlKeyString3+ ' ' + urlString
                    urls.append({urlKey : urlString})
                    
        print json.dumps(urls, ensure_ascii=False, encoding='UTF-8')
    finally:
         file_object.close()
    
    

    将上述两个文件生成的json放在一起,然后拼接最基本的postman的200断言
    apiToPostman.py

    
    # coding=utf-8
    
    import json
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    apiDicts = \
    [上面两个脚本生成的字典合集]
    
    outputFilePath = "/Users/mac/Desktop/allTest.postman_collection.json"
    itemDicts = []
    
    for apiDict in apiDicts: 
        firstKey = apiDict.keys()[0]
        urlString = apiDict[firstKey]
        rawString = "{{base}}" + urlString
        paths = urlString.split('/')
        itemDict = { "name": firstKey, 
                    "event": [
                                {"listen": "test",
                                "script": {
                                    "id": "14d7825c-fef1-4d4a-a7b1-bdce259f7b41",
                                    "type": "text/javascript",
                                    "exec": [
                                        "pm.test(\"接口返回 200\", function () {",
                                        "    pm.response.to.have.status(200);",
                                        "});"
                                        ]
                                        }
                                        }
                                        ],
                                        "request": {
                                            "method": "POST",
                                            "header": [],
                                            "body": {},
                                            "url": {
                                                "raw": rawString,
                                                "host": [
                                                    "{{base}}"
                                                    ],
                                                "path": paths
                                            }
                            },
                            "response": []
                            }
        itemDicts.append(itemDict)
    
    postmanDict = {
        "info": {
            "_postman_id": "bb6f7282-9c68-4b73-a463-d2087e3301c5",
            "name": "PythonTest",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item":itemDicts
            }
    jsonStr = json.dumps( postmanDict, ensure_ascii=False, encoding='UTF-8')
    with open(outputFilePath, 'wt') as f:
        f.write(jsonStr)
    
    
    

    那么问题又来了,之前我接了两百个接口,里面的断言逻辑已经写好了,除了判断HTTP本身的200,还有我们服务器内部定义的成功的状态,或者某种我认为的成功的状态。
    然后,这上面的脚本又生成了新的。这里面新的会有之前写好的里面的,也会有没有,怎么判断这个没有呢。

    于是我又对上面的脚本进行了更改,加入了一个bool值用于判断是否需要进入merge之前的老json文件

    
    # coding=utf-8
    
    import json
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    isNeedMergeOldJSON = False
    if isNeedMergeOldJSON:
        with open("/Users/mac/Desktop/companyProj/QYD_InterfaceAutomationTest/QYDWithParameter.postman_collection.json",'r') as load_f:
            oldPostmanDict = json.load(load_f)
            
    
    
    apiDicts = \
    [xcode和weexulr生成的url串]
    
    
    
    
    outputFilePath = "/Users/mac/Desktop/postman.json"
    
    itemDicts = []
    if isNeedMergeOldJSON:
        itemDicts = oldPostmanDict['item']
        oldURLStrings =[]
        for itemDic in itemDicts:
            oldURLStrings.append(itemDic['request']['url']['raw'])
    for apiDict in apiDicts: 
        firstKey = apiDict.keys()[0]
        urlString = apiDict[firstKey]
        rawString = "{{base}}" + urlString
        paths = urlString.split('/')
        itemDict = { "name": firstKey, 
                    "event": [
                                {"listen": "test",
                                "script": {
                                    "id": "14d7825c-fef1-4d4a-a7b1-bdce259f7b41",
                                    "type": "text/javascript",
                                    "exec": [
                                        "pm.test(\"接口返回 200\", function () {",
                                        "    pm.response.to.have.status(200);",
                                        "});"
                                        ]
                                        }
                                        }
                                        ],
                                        "request": {
                                            "method": "POST",
                                            "header": [],
                                            "body": {},
                                            "url": {
                                                "raw": rawString,
                                                "host": [
                                                    "{{base}}"
                                                    ],
                                                "path": paths
                                            }
                            },
                            "response": []
                            }
        if isNeedMergeOldJSON:
            wantJoinURLString = itemDict['request']['url']['raw']
            value =  wantJoinURLString.replace('{{base}}','') 
            if wantJoinURLString in oldURLStrings:
                pass
            else:
                itemDicts.append(itemDict)
        else:
            itemDicts.append(itemDict)
    postmanDict = {
        "info": {
            "_postman_id": "bb6f7282-9c68-4b73-a463-d2087e3301c5",
            "name": "PythonTest",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item":itemDicts
            }
    jsonStr = json.dumps( postmanDict, ensure_ascii=False, encoding='UTF-8')
    with open(outputFilePath, 'wt') as f:
        f.write(jsonStr)
    

    相关文章

      网友评论

        本文标题:python 读取文本实践:读取文件中的url及描述,拼接成字典

        本文链接:https://www.haomeiwen.com/subject/biuajftx.html