Пример #1
0
def store_all_testcase_to_db(proj='AutoTest'):  
    datainfo=files.read_json('mysql_conn', 'mysql')
    conn=mysql_opr.get_connection(serverip = datainfo['host'],
        port = int(datainfo['port']),
        account = datainfo['user'],
        password = datainfo['password'],
        db_name='qateam')
    sql0="insert into auto_ui_case_updateversion(update_version,proj_name) VALUES('{0}','{1}')".format(current_time,proj)
    mysql_opr.query_mysql2(conn,sql0)
    
    sql1="""SELECT * FROM
            (SELECT a.id,a.py_package,a.run_env,a.platform,a.sub_package,c.root_dir,c.name from auto_ui_collection a
             LEFT JOIN auto_ui_project c ON a.py_project=c.id) b
             WHERE b.name='{}'""".format(proj)
    package_list=mysql_opr.select_from_mysql(conn, sql1, 0)['data'] 
    for pk in package_list:
        if pk['sub_package']==0:
            get_pyfiles_in_packages(pk['root_dir'],pk['py_package'],pk['run_env'],pk['platform'],pk['id'],conn=conn,find_sub=True)
        else:
            sql2="""SELECT * FROM
                    (SELECT a.id,a.run_env,a.platform,a.sub_package,c.root_dir,c.name,d.py_package,d.id AS moduleid
                     FROM auto_ui_collection a
                     LEFT JOIN auto_ui_project c ON a.py_project=c.id
                     LEFT JOIN auto_ui_businessmodule d ON a.id=d.collection
                    ) b
                     WHERE b.name='{}'
                     AND b.sub_package=1""".format(proj)
            package_list1=mysql_opr.select_from_mysql(conn, sql2, 0)['data'] 
            print('package_list1: ',package_list1)
            for sub_pk in package_list1:
                get_pyfiles_in_packages(sub_pk['root_dir'],sub_pk['py_package'],
                                        sub_pk['run_env'],sub_pk['platform'],sub_pk['id'],
                                        conn=conn,module_id=sub_pk['moduleid'],find_sub=False)
Пример #2
0
 def get(self,request):
     #print(request.COOKIES)
     menu_active='webui'
     query0 = 'select * from auto_ui_collection where run_env=0 and ui_sys=0'
     query1 = 'select * from auto_ui_collection where run_env=2 and ui_sys=0'
     conn=mysql_opr.get_connection_qadb('qateam')
     test_menus=mysql_opr.select_from_mysql(conn,query0,total=0)['data']
     prd_menus=mysql_opr.select_from_mysql(conn,query1,total=0)['data']
     #print(locals())
     return render(request, 'yiqa_webui.html', locals())
Пример #3
0
def query_testcase_list(request):
    ''''''
    collectionid=request.POST['collectionid']
    moduleid=request.POST['moduleid']
    tapdid=request.POST['tapdid']
    keyword=request.POST['keyword']
    print('moduleid:',moduleid)
    print('tapdid:',tapdid)
    print('keyword:',keyword)
    #query1 = 'select * from auto_ui_testcase where collection='+str(collectionid)
    query1 = """SELECT a.*,b.id AS module_id,b.name AS module_name,c.run_total,c.avg FROM auto_ui_testcase a
            LEFT JOIN auto_ui_businessmodule b 
            ON a.business_module=b.id
            LEFT JOIN (
                SELECT COUNT(id) AS run_total,CAST(AVG(test_duration) AS DECIMAL(10,2)) AS avg,test_name FROM uitest_tests
              GROUP BY test_name
            ) c ON a.py_name=c.test_name
            WHERE a.collection="""+str(collectionid)
    if tapdid:
        query1+=' and tapd_id='+tapdid
    if moduleid:
        query1+=' and business_module='+moduleid
    if keyword:
        query1+=' and py_desc like \'%'+keyword+'%\''
    print('query1:',query1)
    conn=mysql_opr.get_connection_qadb('qateam')
    rsp_data=mysql_opr.select_from_mysql(conn,query1,total=0)
    return JsonResponse(rsp_data, safe=False) 
Пример #4
0
def send_mail(subject, content, to_users):
    '''
    @param to_users: list eg.['*****@*****.**','*****@*****.**']
    '''
    #account = files.read_txt('local_account','yw_local')
    #sender = account[0]+'@111.com.cn'
    query = 'SELECT * FROM smtp_config ORDER BY id DESC LIMIT 1'
    conn = mysql_opr.get_connection('serverip', 'port', 'db_name', 'account',
                                    'password')
    rsp_data = mysql_opr.select_from_mysql(conn, query, 1)
    if rsp_data['code'] != 0:
        raise Exception('查询邮件发送者出现异常:' + rsp_data['msg'])
    account = rsp_data['data']
    sender = account['sender_name'] + account['sender_suffix']

    print('sender:', sender)
    msg = MIMEText(content, 'html', 'utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = sender
    msg['To'] = ','.join(to_users)
    #msg['To'] = to_users
    # msg['Cc'] = cc_user

    server = smtplib.SMTP()
    # server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)
    # server.set_debuglevel(1)
    server.connect("smtp.exmail.qq.com")  #此处以腾讯企业邮箱为例
    server.starttls()
    server.login(sender, account['sender_pwd'])
    print('邮箱登录成功')

    senders = server.sendmail(sender, to_users, msg.as_string())
    print(senders)
    server.quit()
    return True
Пример #5
0
def update_case_estimatetime(request):
    '''更新运行时间'''
    py_name=request.POST['py_name']
    query1 = "SELECT AVG(test_duration) as avg FROM uitest_tests WHERE test_name='"+py_name+"'"
    conn=mysql_opr.get_connection_qadb('qateam')
    rsp_data=mysql_opr.select_from_mysql(conn,query1,total=1)
    if rsp_data['code']==0:
        if rsp_data['data']['avg']:
            query2="UPDATE auto_ui_testcase SET estimate_time={0} WHERE py_name='{1}'".format(rsp_data['data']['avg'],
                                                                                              py_name)
            mysql_opr.query_mysql2(conn,query2,close=True)
    return JsonResponse(rsp_data, safe=False) 
Пример #6
0
def get_collection_info(request):
    ''''''
    collectionid=request.POST['collectionid']
    query0 = 'select name,run_env from auto_ui_collection where id='+str(collectionid)
    #query1 = 'select * from auto_ui_testcase where collection='+str(collectionid)
    query1 = """SELECT a.*,b.id AS module_id,b.name AS module_name,c.run_total,c.avg FROM auto_ui_testcase a
            LEFT JOIN auto_ui_businessmodule b 
            ON a.business_module=b.id
            LEFT JOIN (
                SELECT COUNT(id) AS run_total,CAST(AVG(test_duration) AS DECIMAL(10,2)) AS avg,test_name FROM uitest_tests
              GROUP BY test_name
            ) c ON a.py_name=c.test_name
            WHERE a.collection="""+str(collectionid)
    query2 = 'select id,name from auto_ui_businessmodule where collection='+str(collectionid)
    conn=mysql_opr.get_connection_qadb('qateam')
    rsp_data1=mysql_opr.select_from_mysql(conn,query0,total=1)
    rsp_data2=mysql_opr.select_from_mysql(conn,query1,total=0)
    rsp_data3=mysql_opr.select_from_mysql(conn,query2,total=0)
       
    return JsonResponse({"collection":rsp_data1['data'],
                         "testcases":rsp_data2['data'],
                         "testmodule":rsp_data3['data']}, safe=False) 
Пример #7
0
 def get_sub_dirs(dir_path, pack_name, sub_dir, depth):
     print('sub_dir: ', sub_dir)
     sub_list = os.listdir(dir_path)
     print('sub_list: ', sub_list)
     for item in sub_list:
         #print('item: ',item)
         item_path = os.sep.join([dir_path, item])
         if depth == 0:
             if os.path.isfile(item_path) and item == '__init__.py':
                 mname, ext = os.path.splitext(item)
                 module = "." + mname
                 #print('type(module): ',type(module))
                 module = importlib.import_module(module, pack_name)
                 #print('inspect.getfile(module):',inspect.getfile(module))
                 #print('module.__name__: ',module.__name__)
                 if module.__doc__:
                     all_pack[pack_name] = module.__doc__.replace('\n', '')
                 else:
                     all_pack[pack_name] = module.__doc__
                 if conn:
                     sql1 = '''select * from auto_ui_businessmodule where py_package='{}'
                             and py_project={}'''.format(
                         pack_name, py_project)
                     res1 = mysql_opr.select_from_mysql(conn, sql1)
                     if res1['code'] == 0 and len(res1['data']) > 0:
                         print('已经存在:', pack_name)
                     else:
                         print('新增子包:', pack_name)
                         sql2 = '''INSERT INTO auto_ui_businessmodule(name,platform,collection,
                         sub_package,py_package,py_project,
                         run_env,ui_sys)
                                 VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')
                                 '''.format(all_pack[pack_name], platform,
                                            coll_id, 1, pack_name,
                                            py_project, 0, 1)
                         mysql_opr.query_mysql2(conn, sql2)
         if depth > 0:
             if os.path.isdir(item_path) and item not in except_dir:
                 sub_dir[item] = {}
                 #print('dirs: ',sub_dir[item])
                 pack_name1 = ('.').join([pack_name, item])
                 get_sub_dirs(item_path, pack_name1, sub_dir[item],
                              depth - 1)
Пример #8
0
def get_module_testcase(package,
                        pyfile,
                        run_env,
                        platform,
                        coll_id,
                        conn=None,
                        module_id=None):
    #filename=pyfile.lower()
    mname, ext = os.path.splitext(pyfile)
    module = "." + mname
    #print('type(module): ',type(module))

    module = importlib.import_module(module, package)
    #testcases=[]
    #print('module.__dict__: ',module.__dict__)
    #print('inspect.getfile(module):',inspect.getfile(module))
    #print('pyfile: ',pyfile)
    print('module.__name__: ', module.__name__)
    file_path = module.__name__.replace('.', '/') + '.py'
    #print('file_path: ',file_path)
    #print('module: ',module.__dict__)
    #print('module: ',help(module))
    #file_path=package.replace('.',os.sep)+os.sep+module.__name__
    #print('sourcecode:')
    #print(inspect.getsource(module))
    print('dir(module): ', dir(module))
    for attr in dir(module):
        if attr.startswith("test"):
            func = getattr(module, attr)
            print('    testcase:', func.__name__)
            pytestmark = []
            py_skip_reason = ''
            tapd_id = ''
            tapd_proj = ''
            cart_order_oprs = 1
            if 'pytestmark' in func.__dict__:
                #print('    pytestmarks:',func.__dict__['pytestmark'])
                #print('    ',func.__dict__['pytestmark'])
                for item in func.__dict__['pytestmark']:
                    if item.__dict__['name'] == 'tapd':
                        if item.__dict__['args']:
                            tapd_str = str(item.__dict__['args'][0]).split('_')
                            if len(tapd_str) > 1:
                                tapd_proj = tapd_str[0]
                                tapd_id = tapd_str[1]
                            else:
                                tapd_id = tapd_str[0]
                    else:
                        pytestmark.append(item.__dict__['name'])
                        if item.__dict__['name'] == 'skip':
                            #print('    py_skip_reason:',item.__dict__)
                            if item.__dict__['args']:
                                py_skip_reason = item.__dict__['args'][0]

                        if '_nocart' in item.__dict__['name']:
                            cart_order_oprs = 0

            #print('    ',func.__doc__)
            #testcases.append(func.__name__)

            py_name = file_path + " ::" + func.__name__

            print('    func content:')
            #print(inspect.getsource(func))
            assert_search = re.findall('assert ', inspect.getsource(func))
            assert_num = len(assert_search)
            print('    func assert_num:', assert_num)

            if conn:
                print('------------------------------')
                if module_id:
                    business_module = module_id
                else:
                    business_module = 0
                sql0 = """SELECT id FROM auto_ui_testcase
                    WHERE py_name='{0}'
                    AND collection={1}""".format(py_name, coll_id)
                res0 = mysql_opr.select_from_mysql(conn,
                                                   sql0,
                                                   total=0,
                                                   close=False)
                if res0['code'] == 0 and len(res0['data']) > 0:
                    print('已有此用例,更新:', py_name)
                    sql1 = '''UPDATE auto_ui_testcase SET py_desc='{0}',cart_order_oprs='{1}',
                            py_marks='{2}',run_env='{3}',platform='{4}',py_skip_reason='{5}',
                            business_module='{6}',tapd_id='{7}',tapd_proj='{8}',update_version='{9}',
                            assert_num={12}
                            WHERE py_name='{10}' AND collection={11}
                            '''.format(func.__doc__.replace('\'', '"'),
                                       cart_order_oprs, ';'.join(pytestmark),
                                       run_env, platform, py_skip_reason,
                                       business_module, tapd_id, tapd_proj,
                                       current_time, py_name, coll_id,
                                       assert_num)
                    mysql_opr.query_mysql2(conn, sql1)
                else:
                    print('无此用例,新增:', py_name)
                    sql2 = '''INSERT INTO auto_ui_testcase(py_name,py_desc,py_module,cart_order_oprs,
                            py_marks,py_file,run_env,platform,collection,py_skip_reason,
                            business_module,tapd_id,tapd_proj,update_version,assert_num)
                            VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7},'{8}','{9}','{10}','{11}','{12}','{13}','{14}')
                            '''.format(py_name, func.__doc__.replace(
                        '\'', '"'), module.__name__, cart_order_oprs,
                                       ';'.join(pytestmark), file_path,
                                       run_env, platform, coll_id,
                                       py_skip_reason, business_module,
                                       tapd_id, tapd_proj, current_time,
                                       assert_num)
                    #print('sql2: ',sql2)
                    mysql_opr.query_mysql2(conn, sql2)