Пример #1
0
class API_XTPZ(unittest.TestCase):
    '''系统配置类api接口测试'''
    @classmethod
    def setUpClass(self):
        self.log=LogTools()
        self.yaml=YamlTools()
        self.req=RequestTools()
        self.log.info('>>>>>>>>>>>>>>>>>>>>>[XTPZ_api_start]<<<<<<<<<<<<<<<<<<<<')

    @classmethod
    def tearDownClass(self):
        self.log.info('>>>>>>>>>>>>>>>>>>>>>[XTPZ_api_end]<<<<<<<<<<<<<<<<<<<<<<')

    def test_001_getSysTime(self):
        '''系统配置类-获取系统时间'''
        casename='获取系统时间'
        api=self.yaml.get_XTPZapi(0)
        # 发送请求
        try:
            result=self.req.get_method(api,casename)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_002_getSysErrorCode(self):
        '''系统配置类-获取系统错误码映射'''
        casename='获取错误码映射'
        api=self.yaml.get_XTPZapi(1)
        # 发送请求
        try:
            result=self.req.get_method(api,casename)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_003_getVersion(self):
        '''系统配置类-获取系统版本信息'''
        data={'innerver':'2000','appid':'iosapp'}
        casename='获取版本信息'
        api=self.yaml.get_XTPZapi(3)
        # 发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)
            self.log.info('[%s]-[Result]:PASS' % casename)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            raise
class API_ResCode(unittest.TestCase):
    '''校验注册验证码api接口测试'''
    def setUp(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.api = CommonApiTools()
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[ResCode_api_start]<<<<<<<<<<<<<<<<<<<<')

    def tearDown(self):
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[ResCode_api_end]<<<<<<<<<<<<<<<<<<<<')

    def test_001_check_registercode(self):
        '''用户登录类-校验注册验证码'''
        casename = '校验注册验证码'
        sql = 'select * from verifycode;'
        api = self.yaml.get_DLapi(3)
        #校验注册验证码
        self.api.get_verifycode(1)
        sqldata = self.db.get_selectdata_row(sql, 1)
        #获取数据
        telnum = sqldata[0]
        res_code = sqldata[1]
        data = {'mobile': telnum, 'validcode': res_code}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['errorMessage'], '验证码正确!')
            self.log.info('[%s]-[Result]:PASS' % casename)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            raise
Пример #3
0
class API_HY(unittest.TestCase):
    '''好友类api接口测试'''
    @classmethod
    def setUpClass(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.api = CommonApiTools()
        self.api.login_api_pwd()  # 密码登陆
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[HY_api_start]<<<<<<<<<<<<<<<<<<<<')

    @classmethod
    def tearDownClass(self):
        self.api.logout_api()  # 注销退出
        self.log.info('>>>>>>>>>>>>>>>>>>>>>[HY_api_end]<<<<<<<<<<<<<<<<<<<<')

    def test_001_friendsinfo_userid(self):
        '''好友类-获取好友信息(通过userid)'''
        casename = '获取好友信息-userid'
        api = self.yaml.get_HYapi(6)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        userid = sqldata[3]
        token = sqldata[6]
        data = {'userid': userid, 'token': token}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['result']['userid'], userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_002_friendinfo_mobile(self):
        '''好友类-获取好友信息(通过mobile)'''
        casename = '获取好友信息-mobile'
        api = self.yaml.get_HYapi(6)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        mobile = sqldata[0]
        data = {'token': token, 'mobile': mobile}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['result']['mobile'], mobile)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_003_friendinfo_useridmobile(self):
        '''好友类-获取好友信息(通过userid/mobile)'''
        casename = '获取好友信息-userid/mobile'
        api = self.yaml.get_HYapi(6)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        userid = sqldata[3]
        mobile = sqldata[0]
        data = {'token': token, 'userid': userid, 'mobile': mobile}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(
                result['errorCode'], 0) and self.assertSequenceEqual(
                    result['isTrue'], True) and self.assertSequenceEqual(
                        result['result']['userid'],
                        userid) and self.assertSequenceEqual(
                            result['result']['mobile'], mobile)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_004_addfriends(self):
        '''好友类-向指定用户申请成为好友'''
        casename = '向指定用户申请成为好友'
        api = self.yaml.get_HYapi(0)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        friendid = 'user10020'
        info = '123'
        data = 'token=%s&friendid=%s&info=%s' % ('123456', friendid, info)
        #发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertSequenceEqual(
                    result['errorMessage'], '申请成功,请耐心等待对方确认。')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_005_myaddlist(self):
        '''好友类-取得向我提出加好友申请的用户列表'''
        casename = '获取添加自己的用户列表'
        api = self.yaml.get_HYapi(1)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        userid = sqldata[3]
        data = {'token': '123456', 'userid': userid}
        #发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 10304) and self.assertEqual(
                result['isTrue'], False) and self.assertSequenceEqual(
                    result['errorMessage'], '用户不存在')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_006_deletefriend(self):
        '''好友类-删除指定好友'''
        casename = '删除指定好友'
        api = self.yaml.get_HYapi(5)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        friendid = 'user10001'
        data = 'token=%s&friendid=%s' % (token, friendid)
        #发送请求
        try:
            result = self.req.delete_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['errorMessage'], '删除好友成功')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_007_getfriendslist(self):
        '''好友类-获得我的好友列表'''
        casename = '获得我的好友列表'
        api = self.yaml.get_HYapi(7)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        userid = sqldata[3]
        data = {'token': token, 'userid': userid}
        #发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 10306) and self.assertEqual(
                result['errorMessage'], '好友不存在')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_008_nickname(self):
        '''好友类-为我的好友设置昵称'''
        casename = '为我的好友设置昵称'
        api = self.yaml.get_HYapi(8)
        sql = 'select * from userlogin;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[6]
        userid = sqldata[3]
        friendid = 'user10033'
        name = '戴眼镜的熊'
        data = 'token=%s&friendid=%s&name=%s' % (token, friendid, name)
        #发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['errorMessage'], '更新昵称成功')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_009_newmessage(self):
        '''好友类-取得待处理好友消息'''
        casename = '取得待处理好友消息'
        api = self.yaml.get_HYapi(11)
        sql = 'select * from userlogin;'
        #获取数据
        token = self.db.get_specific_data(sql, 0, 6)
        data = {'token': token}
        #发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 10811) and self.assertEqual(
                result['errorMessage'], '没有新的朋友消息')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_010_search(self):
        '''好友类-搜索引擎'''
        casename = '搜索引擎'
        api = self.yaml.get_HYapi(12)
        sql = 'select * from userlogin;'
        #获取数据
        token = self.db.get_specific_data(sql, 0, 6)
        type = 'all'
        key = '王'
        data = {'token': token, 'type': type, 'key': key}
        #发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            raise
Пример #4
0
class ExcelTestTools:
    def __init__(self, reportname=None):
        '''初始化'''
        datafile = os.path.join(data_path, 'Data_API.xlsx')  #定位API数据文件
        self.readexcel = ExcelReadTools(datafile)
        self.writeexcel = ExcelWriteTools(reportname)
        self.req = RequestTools()
        self.log = LogTools()
        #self.db=DataBaseTools()
        #统计测试用例数量信息

    def get_sheet_nrows(self, sheetname):
        '''
        获取sheet行数
        :param sheetname: 工作表sheet
        :return: 行数
        '''
        return self.readexcel.get_nrows(sheetname)

    def read_sheet(self, sheetname):
        '''
        读取指定sheet中的每行的数据信息并保存在字典中
        :param sheetname: sheet名称
        :return: 返回一个装有对应sheet的所有参数数据字典
        '''
        data_dict = {}
        #获取指定sheet的行数
        getrows = self.get_sheet_nrows(sheetname)
        #获取每行的值
        for i in range(1, getrows):
            get_data = self.readexcel.get_row_values(sheetname, i)  #获取当行
            data_dict[i] = get_data  #行数:参数值
        return data_dict

    def get_param_dict(self, sheetname, key):
        '''
        获取data_dict中指定key对应的参数
        :param data_dict: 数据字典
        :param key: key值(1、2、3...)
        :return: 参数字典
        '''
        param_dict = {}
        data_dict = self.read_sheet(sheetname)
        param_list = data_dict[key]
        #获取参数组合成字典
        param_dict['test_class'] = param_list[0]
        param_dict['test_title'] = param_list[1]
        param_dict['api'] = param_list[2]
        param_dict['request_type'] = param_list[3]
        param_dict['post_type'] = param_list[4]
        param_dict['url_param'] = param_list[5]
        param_dict['data_param'] = param_list[6]
        param_dict['headers'] = param_list[7]
        param_dict['errorCode'] = param_list[8]
        param_dict['isTrue'] = param_list[9]
        param_dict['errorMessage'] = param_list[10]
        return param_dict

    def send_request(self, param_dict, worksheet, cellid):
        '''
        根据参数字典发送请求
        :param param_dict : 参数字典
        :param worksheet : 工作sheet
        '''
        #统计用例总数
        self.sumnum += 1
        #获取参数字典各类参数值
        test_class = param_dict['test_class']
        test_title = param_dict['test_title']
        api = param_dict['api']
        request_type = param_dict['request_type']
        post_type = param_dict['post_type']
        url_param = param_dict['url_param']
        data_param = param_dict['data_param']
        headers = param_dict['headers']
        errorCode = param_dict['errorCode']
        isTrue = param_dict['isTrue']
        errorMessage = param_dict['errorMessage']

        #转换url_param参数
        if url_param == '':
            url_param = None
        elif '=' in url_param:
            url_param = url_param
        else:
            url_param = eval(url_param)
        #转换data_param参数
        if data_param == '':
            data_param = None
        else:
            data_param = eval(data_param)
        #转换errorCode'
        errorCode = int(errorCode)
        #转换isTrue参数
        if isTrue == 'True':
            isTrue = True
        elif isTrue == 'False':
            isTrue = False

        #设定单元格样式
        body_format = self.writeexcel.set_cellformat(font_size=10)

        #判断发送类型并调用请求函数
        #get请求
        if request_type == 'get':
            #发送请求
            try:
                result = self.req.get_method(api, test_title, url_param)
                if result['errorCode'] == errorCode and result[
                        'isTrue'] == isTrue and result[
                            'errorMessage'] == errorMessage:
                    self.log.info('[%s]-[Result]:PASS' % test_title)
                    self.log.info('-' * 60)
                    #插入相关结果到excel表
                    self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                               str(result), body_format)  #响应信息
                    self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                               result['errorCode'],
                                               body_format)  #实际errorCode
                    self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                               result['isTrue'],
                                               body_format)  #实际isTrue
                    self.writeexcel.write_cell(worksheet, 'O%d' % cellid,
                                               result['errorMessage'],
                                               body_format)  #实际errorMessage
                    self.writeexcel.write_pass(worksheet,
                                               'P%d' % cellid)  #PASS
                    self.successnum += 1  #记录成功用例个数
                else:
                    self.log.error('[%s]-[Result]:FAILED' % test_title)
                    self.log.info('-' * 60)
                    # 插入相关结果到excel表
                    self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                               str(result), body_format)  #响应信息
                    self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                               result['errorCode'],
                                               body_format)  # 实际errorCode
                    self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                               result['isTrue'],
                                               body_format)  # 实际isTrue
                    self.writeexcel.write_cell(worksheet, 'O%d' % cellid,
                                               result['errorMessage'],
                                               body_format)  # 实际errorMessage
                    self.writeexcel.write_fail(worksheet,
                                               'P%d' % cellid)  # FAIL
                    self.failnum += 1  #记录失败用例个数
            except Exception as e:
                self.writeexcel.write_na(worksheet, 'P%d' % cellid)  #N/A
                self.log.error('[%s]-[Error]:%s' % (test_title, e))
        #put请求
        elif request_type == 'put':
            #发送请求
            try:
                result = self.req.put_method(api, test_title, data_param,
                                             url_param)
                if result['errorCode'] == errorCode and result[
                        'isTrue'] == isTrue and result[
                            'errorMessage'] == errorMessage:
                    self.log.info('[%s]-[Result]:PASS' % test_title)
                    self.log.info('-' * 60)
                    # 插入相关结果到excel表
                    self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                               str(result),
                                               body_format)  # 响应信息
                    self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                               result['errorCode'],
                                               body_format)  # 实际errorCode
                    self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                               result['isTrue'],
                                               body_format)  # 实际isTrue
                    self.writeexcel.write_cell(worksheet, 'O%d' % cellid,
                                               result['errorMessage'],
                                               body_format)  # 实际errorMessage
                    self.writeexcel.write_pass(worksheet,
                                               'P%d' % cellid)  # PASS
                    self.successnum += 1
                else:
                    self.log.error('[%s]-[Result]:FAILED' % test_title)
                    self.log.info('-' * 60)
                    # 插入相关结果到excel表
                    self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                               str(result),
                                               body_format)  # 响应信息
                    self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                               result['errorCode'],
                                               body_format)  # 实际errorCode
                    self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                               result['isTrue'],
                                               body_format)  # 实际isTrue
                    self.writeexcel.write_cell(worksheet, 'O%d' % cellid,
                                               result['errorMessage'],
                                               body_format)  # 实际errorMessage
                    self.writeexcel.write_fail(worksheet,
                                               'P%d' % cellid)  # FAIL
                    self.failnum += 1
            except Exception as e:
                self.writeexcel.write_na(worksheet, 'P%d' % cellid)  # N/A
                self.log.error('[%s]-[Error]:%s' % (test_title, e))
        #delete请求
        elif request_type == 'delete':
            #发送请求
            try:
                result = self.req.delete_method(api, test_title, url_param)
                if result['errorCode'] == errorCode and result[
                        'isTrue'] == isTrue and result[
                            'errorMessage'] == errorMessage:
                    self.log.info('[%s]-[Result]:PASS' % test_title)
                    self.log.info('-' * 60)
                    # 插入相关结果到excel表
                    self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                               str(result),
                                               body_format)  # 响应信息
                    self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                               result['errorCode'],
                                               body_format)  # 实际errorCode
                    self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                               result['isTrue'],
                                               body_format)  # 实际isTrue
                    self.writeexcel.write_cell(worksheet, 'O%d' % cellid,
                                               result['errorMessage'],
                                               body_format)  # 实际errorMessage
                    self.writeexcel.write_pass(worksheet,
                                               'P%d' % cellid)  # PASS
                    self.successnum += 1
                else:
                    self.log.error('[%s]-[Result]:FAILED' % test_title)
                    self.log.info('-' * 60)
                    # 插入相关结果到excel表
                    self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                               str(result),
                                               body_format)  # 响应信息
                    self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                               result['errorCode'],
                                               body_format)  # 实际errorCode
                    self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                               result['isTrue'],
                                               body_format)  # 实际isTrue
                    self.writeexcel.write_cell(worksheet, 'O%d' % cellid,
                                               result['errorMessage'],
                                               body_format)  # 实际errorMessage
                    self.writeexcel.write_fail(worksheet,
                                               'P%d' % cellid)  # FAIL
                    self.failnum += 1
            except Exception as e:
                self.writeexcel.write_na(worksheet, 'P%d' % cellid)  # N/A
                self.log.error('[%s]-[Error]:%s' % (test_title, e))
        #post请求
        elif request_type == 'post':
            # post类型为urlencode
            if post_type == 'urlencode':
                try:
                    result = self.req.post_method('urlencode', api, test_title,
                                                  data_param, url_param)
                    if result['errorCode'] == errorCode and result[
                            'isTrue'] == isTrue and result[
                                'errorMessage'] == errorMessage:
                        self.log.info('[%s]-[Result]:PASS' % test_title)
                        self.log.info('-' * 60)
                        # 插入相关结果到excel表
                        self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                                   str(result),
                                                   body_format)  # 响应信息
                        self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                                   result['errorCode'],
                                                   body_format)  # 实际errorCode
                        self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                                   result['isTrue'],
                                                   body_format)  # 实际isTrue
                        self.writeexcel.write_cell(
                            worksheet, 'O%d' % cellid, result['errorMessage'],
                            body_format)  # 实际errorMessage
                        self.writeexcel.write_pass(worksheet,
                                                   'P%d' % cellid)  # PASS
                        self.successnum += 1
                    else:
                        self.log.error('[%s]-[Result]:FAILED' % test_title)
                        self.log.info('-' * 60)
                        # 插入相关结果到excel表
                        self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                                   str(result),
                                                   body_format)  # 响应信息
                        self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                                   result['errorCode'],
                                                   body_format)  # 实际errorCode
                        self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                                   result['isTrue'],
                                                   body_format)  # 实际isTrue
                        self.writeexcel.write_cell(
                            worksheet, 'O%d' % cellid, result['errorMessage'],
                            body_format)  # 实际errorMessage
                        self.writeexcel.write_fail(worksheet,
                                                   'P%d' % cellid)  # FAIL
                        self.failnum += 1
                except Exception as e:
                    self.writeexcel.write_na(worksheet, 'P%d' % cellid)  # N/A
                    self.log.error('[%s]-[Error]:%s' % (test_title, e))
            # post类型为form
            elif post_type == 'form':
                try:
                    result = self.req.post_method('form', api, test_title,
                                                  data_param, url_param)
                    if result['errorCode'] == errorCode and result[
                            'isTrue'] == isTrue and result[
                                'errorMessage'] == errorMessage:
                        self.log.info('[%s]-[Result]:PASS' % test_title)
                        self.log.info('-' * 60)
                        # 插入相关结果到excel表
                        self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                                   str(result),
                                                   body_format)  # 响应信息
                        self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                                   result['errorCode'],
                                                   body_format)  # 实际errorCode
                        self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                                   result['isTrue'],
                                                   body_format)  # 实际isTrue
                        self.writeexcel.write_cell(
                            worksheet, 'O%d' % cellid, result['errorMessage'],
                            body_format)  # 实际errorMessage
                        self.writeexcel.write_pass(worksheet,
                                                   'P%d' % cellid)  # PASS
                        self.successnum += 1
                    else:
                        self.log.error('[%s]-[Result]:FAILED' % test_title)
                        self.log.info('-' * 60)
                        # 插入相关结果到excel表
                        self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                                   str(result),
                                                   body_format)  # 响应信息
                        self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                                   result['errorCode'],
                                                   body_format)  # 实际errorCode
                        self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                                   result['isTrue'],
                                                   body_format)  # 实际isTrue
                        self.writeexcel.write_cell(
                            worksheet, 'O%d' % cellid, result['errorMessage'],
                            body_format)  # 实际errorMessage
                        self.writeexcel.write_fail(worksheet,
                                                   'P%d' % cellid)  # FAIL
                        self.failnum += 1
                except Exception as e:
                    self.writeexcel.write_na(worksheet, 'P%d' % cellid)  # N/A
                    self.log.error('[%s]-[Error]:%s' % (test_title, e))
            # post类型为json
            elif post_type == 'json':
                try:
                    result = self.req.post_method('json', api, test_title,
                                                  data_param, url_param)
                    if result['errorCode'] == errorCode and result[
                            'isTrue'] == isTrue and result[
                                'errorMessage'] == errorMessage:
                        self.log.info('[%s]-[Result]:PASS' % test_title)
                        self.log.info('-' * 60)
                        # 插入相关结果到excel表
                        self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                                   str(result),
                                                   body_format)  # 响应信息
                        self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                                   result['errorCode'],
                                                   body_format)  # 实际errorCode
                        self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                                   result['isTrue'],
                                                   body_format)  # 实际isTrue
                        self.writeexcel.write_cell(
                            worksheet, 'O%d' % cellid, result['errorMessage'],
                            body_format)  # 实际errorMessage
                        self.writeexcel.write_pass(worksheet,
                                                   'P%d' % cellid)  # PASS
                        self.successnum += 1
                    else:
                        self.log.error('[%s]-[Result]:FAILED' % test_title)
                        self.log.info('-' * 60)
                        # 插入相关结果到excel表
                        self.writeexcel.write_cell(worksheet, 'I%d' % cellid,
                                                   str(result),
                                                   body_format)  # 响应信息
                        self.writeexcel.write_cell(worksheet, 'K%d' % cellid,
                                                   result['errorCode'],
                                                   body_format)  # 实际errorCode
                        self.writeexcel.write_cell(worksheet, 'M%d' % cellid,
                                                   result['isTrue'],
                                                   body_format)  # 实际isTrue
                        self.writeexcel.write_cell(
                            worksheet, 'O%d' % cellid, result['errorMessage'],
                            body_format)  # 实际errorMessage
                        self.writeexcel.write_fail(worksheet,
                                                   'P%d' % cellid)  # FAIL
                        self.failnum += 1
                except Exception as e:
                    self.writeexcel.write_na(worksheet, 'P%d' % cellid)  # N/A
                    self.log.error('[%s]-[Error]:%s' % (test_title, e))
            else:
                print('POST请求类型设置错误!')
        else:
            print('无对应请求的封装函数,请核实!')

    def write_cover_info(self, worksheet, title, sumnum, successnum, failnum,
                         skipnum, usetime):
        '''
        构造封面
        :param title: 封面主题
        :param sumnum: 用例总数
        :param successnum: 成功用例数量
        :param failnum: 失败用例数量
        :param skipnum: 跳过用例数量
        :param usetime: 执行时间
        '''
        self.writeexcel.create_cover_gui(worksheet, title, sumnum, successnum,
                                         failnum, skipnum, usetime)

    def write_basic_info(self, sheetname):
        '''
        将基本信息写入到指定的sheet中
        :param sheetname: 工作表名称
        '''
        data_list = []
        #读取对应sheet的所有数据信息
        sumrows = self.readexcel.get_nrows(sheetname)
        #循环获取每行的数据(字典类型),然后存放于列表中
        for i in range(1, sumrows):
            param_dict = self.get_param_dict(sheetname, i)
            data_list.append(param_dict)
        #创建sheet
        self.sheet = self.writeexcel.add_worksheet(sheetname)
        #写入基础信息框架
        self.writeexcel.create_table_gui(self.sheet)
        #设定单元格样式
        body_format = self.writeexcel.set_cellformat(font_size=10)
        #循环写入信息
        for k in range(1, len(data_list) + 1):
            self.writeexcel.write_cell(self.sheet, 'A%d' % (k + 1), k,
                                       body_format)  #用例ID
            self.writeexcel.write_cell(self.sheet, 'B%d' % (k + 1),
                                       data_list[k - 1]['test_class'],
                                       body_format)  #模块名称
            self.writeexcel.write_cell(self.sheet, 'C%d' % (k + 1),
                                       data_list[k - 1]['test_title'],
                                       body_format)  #用例内容
            self.writeexcel.write_cell(self.sheet, 'D%d' % (k + 1),
                                       data_list[k - 1]['api'],
                                       body_format)  #URL
            self.writeexcel.write_cell(self.sheet, 'E%d' % (k + 1),
                                       data_list[k - 1]['request_type'],
                                       body_format)  #请求方式
            self.writeexcel.write_cell(self.sheet, 'F%d' % (k + 1),
                                       data_list[k - 1]['headers'],
                                       body_format)  #headers
            self.writeexcel.write_cell(self.sheet, 'G%d' % (k + 1),
                                       data_list[k - 1]['url_param'],
                                       body_format)  #url参数
            self.writeexcel.write_cell(self.sheet, 'H%d' % (k + 1),
                                       data_list[k - 1]['data_param'],
                                       body_format)  #data参数
            self.writeexcel.write_cell(self.sheet, 'J%d' % (k + 1),
                                       data_list[k - 1]['errorCode'],
                                       body_format)  #预期errorCode
            self.writeexcel.write_cell(self.sheet, 'L%d' % (k + 1),
                                       data_list[k - 1]['isTrue'],
                                       body_format)  #预期isTrue
            self.writeexcel.write_cell(self.sheet, 'N%d' % (k + 1),
                                       data_list[k - 1]['errorMessage'],
                                       body_format)  #预期errorMessage
        return self.sheet

    def do_excel_alltests(self, sheetname):
        '''
        执行excel所有的测试用例,步骤如下:
        1、通过sheetname参数去默认的路径下生成ExcelReport文件,并构建sheet框架
        2、通过sheetname参数去数据驱动Data_API.xlsx文件中找到对应的sheet并获取总行数
        3、根据总行数循环获取Data_API.xlsx对应sheet的数据,对每行数据进行数据重组为param_dict
        4、send_request对param_dict的数据进行抓取,同时组合接口请求参数发起接口请求(判断请求类型)
        5、send_request对返回值与excel表的预期值进行对比,对比通过则pass,不通过则fail
        6、send_request将返回值中的相关要素插入到ExcelReport中对应的sheet中
        :param sheetname: 工作表名称
        :return:返回sumnum总执行用例、successnum成功用例、failnum失败用例
        '''
        try:
            sheet = self.write_basic_info(sheetname)
            nrows = self.get_sheet_nrows(sheetname)
            for i in range(1, nrows):
                param_dict = self.get_param_dict(sheetname, i)
                self.send_request(param_dict, sheet, i + 1)
            #保存数据
            num_dict = {}
            num_dict['sumnum'] = self.sumnum
            num_dict['successnum'] = self.successnum
            num_dict['failnum'] = self.failnum
            #初始化数据
            self.sumnum = 0
            self.successnum = 0
            self.failnum = 0
            return num_dict
        except Exception:
            print('执行失败,请检查sheetname是否在Data_API.xlsx中存在.')
            raise

    def closeTools(self):
        '''关闭ExcelWriter'''
        self.writeexcel.close_workbook()
class API_HDGGYY(unittest.TestCase):
    '''活动广告运营api接口测试'''
    @classmethod
    def setUpClass(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.api = CommonApiTools()
        self.api.login_api_pwd()  # 密码登陆
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[SJCJ_api_start]<<<<<<<<<<<<<<<<<<<<')

    @classmethod
    def tearDownClass(self):
        self.api.logout_api()  # 注销退出
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[SJCJ_api_end]<<<<<<<<<<<<<<<<<<<<')

    def test_001_getadv(self):
        '''活动广告运营类-取得当前用户相关的广告列表'''
        casename = '获取当前用户广告列表'
        api = self.yaml.get_HDGGYYapi(0)
        sql = 'select * from userlogin;'
        #获取数据
        token = self.db.get_specific_data(sql, 0, 6)
        data = {'token': token}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_002_signin(self):
        '''活动广告运营类-用户签到,并获得连续打卡天数'''
        casename = '获取用户连续打卡天数'
        api = self.yaml.get_HDGGYYapi(1)
        sql = 'select * from userlogin;'
        # 获取数据
        token = self.db.get_specific_data(sql, 0, 6)
        data = 'token=%s' % token
        # 发送请求
        try:
            result = self.req.post_method('urlencode', api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_003_sendfeedback(self):
        '''活动广告运营类-用户提交问题建议'''
        casename = '用户提交问题建议'
        api = self.yaml.get_HDGGYYapi(2)
        sql = 'select * from userlogin;'
        #获取数据
        userid = self.db.get_specific_data(sql, 0, 3)
        contactinfo = 'QQ:14298246'
        content = '接口测试!'
        data = {
            'userid': userid,
            'contactinfo': contactinfo,
            'content': content
        }
        # 发送请求
        try:
            result = self.req.post_method('json', api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['result'], userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_004_getfeedback(self):
        '''活动广告运营类-用户取得自己提交的问题的反馈'''
        casename = '用户取得自己提交的问题的反馈'
        api = self.yaml.get_HDGGYYapi(3)
        sql = 'select * from userlogin;'
        #获取数据
        token = self.db.get_specific_data(sql, 0, 6)
        data = {'token': token}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库信息
            sql2 = 'update feedback set feedid=\'%s\' where userid=\'%s\';' % (
                result['result'][0]['feedid'], result['result'][0]['userid'])
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_005_gettrack(self):
        '''活动广告运营类-获取建议回复信息'''
        casename = '获取建议回复信息'
        api = self.yaml.get_HDGGYYapi(4)
        sql = 'select * from feedback;'
        #获取数据
        feedid = self.db.get_specific_data(sql, 0, 2)
        data = {'feedid': feedid}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_006_givestar(self):
        '''活动广告运营类-给予客服评级'''
        casename = '给予客服评级'
        api = self.yaml.get_HDGGYYapi(5)
        sql = 'select * from feedback;'
        #获取数据
        feedid = self.db.get_specific_data(sql, 0, 2)
        rate = random.randint(1, 6)  #1-5星评级
        data = 'feedid=%s&rate=%s' % (feedid, rate)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            raise
Пример #6
0
class API_PDDT(unittest.TestCase):
    '''频道电台类api接口测试'''
    @classmethod
    def setUpClass(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.api = CommonApiTools()
        self.api.login_api_pwd()  # 密码登陆
        self.log.info('>>>>>>>>>>>>>>>>>>>>>[PDDT_api_start]<<<<<<<<<<<<<<<<<<<<')

    @classmethod
    def tearDownClass(self):
        self.api.logout_api()  # 注销退出
        self.log.info('>>>>>>>>>>>>>>>>>>>>>[PDDT_api_end]<<<<<<<<<<<<<<<<<<<<')

    def test_001_makechannel(self):
        '''频道电台类-创建频道(普通频道)'''
        casename='创建频道(普通频道)'
        api=self.yaml.get_PDDTapi(0)
        sql='select * from userlogin;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[6]
        userid=sqldata[3]
        data={"channelid" : "","channelname" : "接口测试","channelremark" : "","autoname" : 0,"ispublic" : 0,"logoid" : "","memberids" : ["user10400","user10430"],"owner" : userid,"token" : token}
        #发送请求
        try:
            result=self.req.post_method('json',api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
            #更新数据库信息--channel
            sql2='update channel set channelid=\'%s\',owner=\'%s\',token=\'%s\',channelname=\'%s\' where cid=1'%\
                 (result['result']['channelid'],result['result']['owner'],result['result']['token'],result['result']['channelname'])
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_002_changenickname(self):
        '''频道电台类-更新自己的昵称'''
        casename='更新自己的昵称'
        api=self.yaml.get_PDDTapi(1)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        userid=sqldata[2]
        name='王大锤'
        data='token=%s&channelid=%s&name=%s'%(token,channelid,name)
        #发送请求
        try:
            result=self.req.put_method(api,casename,data=data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True) and self.assertEqual(result['result'],userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_003_changechannelname(self):
        '''频道电台类-管理员更新频道名称'''
        casename='管理员更新频道名称'
        api=self.yaml.get_PDDTapi(2)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        userid=sqldata[2]
        name='我心飞翔'
        data='token=%s&channelid=%s&name=%s'%(token,channelid,name)
        #发送请求
        try:
            result=self.req.put_method(api,casename,data=data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True) and self.assertEqual(result['result'],userid)
            self.log.info('[%s]-[nfResult]:PASS'%casename)
            self.log.info('-'*60)
            #更新数据库--channel
            sql2='update channel set channelname=\'%s\' where owner=\'%s\';'%(name,userid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-' * 60)
            raise

    def test_004_changeremark(self):
        '''频道电台类-管理员更新频道简介'''
        casename='管理员更新频道简介'
        api=self.yaml.get_PDDTapi(3)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        userid=sqldata[2]
        remark='简介更新,接口测试!'
        data='token=%s&channelid=%s&remark=%s'%(token,channelid,remark)
        #发送请求
        try:
            result=self.req.put_method(api,casename,data=data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True) and self.assertEqual(result['result'],userid)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
            #更新数据库--channel
            sql2='update channel set remark=\'%s\' where owner=\'%s\';'%(remark,userid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_005_ispublic_yes(self):
        '''频道电台类-管理员更新频道不需验证申请'''
        casename='管理员更新频道不需验证申请'
        api=self.yaml.get_PDDTapi(4)
        sql='select * from channel;'
        # 获取数据
        sqldata=self.db.get_selectdata_row(sql, 0)
        token=sqldata[3]
        channelid=sqldata[1]
        userid=sqldata[2]
        ispublic=1
        data='token=%s&channelid=%s&ispublic=%s'%(token,channelid,ispublic)
        #发送请求
        try:
            result=self.req.put_method(api,casename,data=data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True) and \
            self.assertEqual(result['errorMessage'],'更新频道成功') and self.assertSequenceEqual(result['result'],userid)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_006_ispublic_no(self):
        '''频道电台类-管理员更新频道需要验证申请'''
        casename = '管理员更新频道需要验证申请'
        api = self.yaml.get_PDDTapi(4)
        sql = 'select * from channel;'
        # 获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[3]
        channelid = sqldata[1]
        userid = sqldata[2]
        ispublic = 0
        data = 'token=%s&channelid=%s&ispublic=%s' % (token, channelid, ispublic)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) and \
            self.assertEqual(result['errorMessage'], '更新频道成功') and self.assertSequenceEqual(result['result'], userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_007_batchadd(self):
        '''频道电台类-批量增加成员到频道中'''
        casename='批量增加成员到频道中'
        api=self.yaml.get_PDDTapi(6)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        userid=sqldata[2]
        body={"channelid" : channelid,"memberlist" : ["user10400","user10430"],"userid" : userid,"token" : token}
        #发送请求
        try:
            result=self.req.put_method(api,casename,body)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True) \
            and self.assertEqual(result['result'],userid)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_008_batchkick(self):
        '''频道电台类-从频道中批量删除成员'''
        casename='从频道中批量删除成员'
        api=self.yaml.get_PDDTapi(7)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        userid=sqldata[2]
        channelid=sqldata[1]
        body={"channelid" : channelid,"memberlist" : ["user10400","user10430"],"userid" : userid,"token" : token}
        #发送请求
        try:
            result=self.req.delete_method(api,casename,body=body)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) \
            and self.assertEqual(result['result'], userid)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_009_applychannel(self):
        '''频道电台类-申请加入频道(需审核)'''
        casename='申请加入频道(需审核)'
        api=self.yaml.get_PDDTapi(8)
        sql = 'select * from channel;'
        # 获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token=sqldata[3]
        userid=sqldata[2]
        channelid='10404'
        reason='申请加入,接口测试!'
        data='token=%s&channelid=%s&reason=%s'%(token,channelid,reason)
        try:
            result=self.req.put_method(api,casename,data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) \
            and self.assertEqual(result['result'], userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_010_addchannel(self):
        '''频道电台类-直接加入频道'''
        casename='直接加入频道'
        api=self.yaml.get_PDDTapi(9)
        sql='select * from channel;'
        #获取数据
        sqldata = self.db.get_selectdata_row(sql, 0)
        token = sqldata[3]
        userid = sqldata[2]
        channelid='10404'
        data='token=%s&channelid=%s'%(token,channelid)
        #发送请求
        try:
            result=self.req.put_method(api,casename,data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) \
            and self.assertEqual(result['result'], userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_011_getmychannellist(self):
        '''频道电台类-取得自己的频道列表'''
        casename='取得我的频道列表'
        api=self.yaml.get_PDDTapi(14)
        sql='select * from channel;'
        #获取数据
        token=self.db.get_specific_data(sql,0,3)
        data={'token':token}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)\
            and self.assertEqual(result['errorMessage'],'操作成功')
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_012_getmember(self):
        '''频道电台类-取得指定频道的成员列表'''
        casename='取得指定频道的成员列表'
        api=self.yaml.get_PDDTapi(15)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        data={'token':token,'channelid':channelid}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)\
            and self.assertEqual(result['errorMessage'],'操作成功')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_013_applylist(self):
        '''频道电台类-获取所有自己创建的频道的相关信息'''
        casename='所有自己创建的频道的相关信息'
        api=self.yaml.get_PDDTapi(16)
        sql='select * from channel;'
        #获取数据
        token=self.db.get_specific_data(sql,0,3)
        data={'token':token}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) \
            and self.assertEqual(result['errorMessage'], '操作成功')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_014_searchchannel(self):
        '''频道电台类-查找频道'''
        casename='查找频道'
        api=self.yaml.get_PDDTapi(17)
        sql='select * from channel;'
        #获取数据
        token=self.db.get_specific_data(sql,0,3)
        key='罗'
        data={'token':token,'key':key}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'])
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_015_randomrecommend(self):
        '''频道电台类-随机推荐频道'''
        casename='随机推荐频道'
        api=self.yaml.get_PDDTapi(18)
        sql='select * from channel;'
        #获取数据
        token=self.db.get_specific_data(sql,0,3)
        maxnum=3
        data={'token':token,'maxnum':maxnum}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'])
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_016_getchannelinfo(self):
        '''频道电台类-获取指定频道详情'''
        casename='获取指定频道详情'
        api=self.yaml.get_PDDTapi(19)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        data={'token':token,'channelid':channelid}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)\
            and self.assertEqual(result['errorMessage'],'操作成功')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_017_getchannelstatus(self):
        '''频道电台类-用户取得自己的频道的活跃统计'''
        casename='用户取得自己的频道的活跃统计'
        api=self.yaml.get_PDDTapi(21)
        sql='select * from channel;'
        #获取数据
        token=self.db.get_specific_data(sql,0,3)
        data={'token':token}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) \
            and self.assertEqual(result['errorMessage'], '操作成功')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_018_getlogo(self):
        '''频道电台类-获取频道logo图片'''
        casename='获取频道logo图片'
        api=self.yaml.get_PDDTapi(22)
        #获取数据
        channelid='10404'
        data={'channelid':channelid}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_019_assertinchannel(self):
        '''频道电台类-判断自己是否在特定频道内'''
        casename='判断自己是否在特定频道内'
        api=self.yaml.get_PDDTapi(23)
        sql='select * from channel;'
        #获取数据
        sqldata=self.db.get_selectdata_row(sql,0)
        token=sqldata[3]
        channelid=sqldata[1]
        data={'token':token,'channelid':channelid}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            #存在时反馈判断
            try:
                self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)\
                and self.assertEqual(result['result'],True)
                self.log.info('[%s]-[Result]:PASS'%casename)
                self.log.info('-'*60)
            #不存在时反馈判断
            except:
                self.assertEqual(result['errorCode'], 0) and self.assertEqual(result['isTrue'], True) \
                and self.assertEqual(result['result'], False)
                self.log.info('[%s]-[Result]:PASS' % casename)
                self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_020_allmember(self):
        '''频道电台类-取得和我相关的所有频道和频道成员列表(不包含自己)'''
        casename='取得和我相关的所有频道和频道成员列表(不包含自己)'
        api=self.yaml.get_PDDTapi(26)
        sql='select * from channel;'
        #获取数据
        token=self.db.get_specific_data(sql,0,3)
        data={'token':'234123414'}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)\
            and self.assertEqual(result['errorMessage'],'操作成功')
            self.log.info('[%s]-[Result]:PASS'%casename)
            self.log.info('-'*60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            self.log.info('-'*60)
            raise

    def test_021_radionews(self):
        '''频道电台类-获取最新路况交通新闻'''
        casename='获取最新路况交通新闻'
        api=self.yaml.get_PDDTapi(27)
        sql='select * from channel;'
        #获取数据
        userid=self.db.get_specific_data(sql,0,2)
        data={'userid':userid}
        #发送请求
        try:
            result=self.req.get_method(api,casename,data)
            self.assertEqual(result['errorCode'],0) and self.assertEqual(result['isTrue'],True)
            self.log.info('[%s]-[Result]:PASS'%casename)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED'%casename)
            raise
Пример #7
0
class ExcelDDTTools:
    def __init__(self):
        '''初始化'''
        datafile = os.path.join(data_path, 'Data_API.xlsx')  # 定位数据文件
        self.readexcel = ExcelReadTools(datafile)
        self.req = RequestTools()
        self.log = LogTools()
        self.db = DataBaseTools()

    def get_ddt_datalist(self,sheetname):
        '''
        获取指定sheet内的所有数据,并组合成ddt_data列表
        :param sheetname: sheet表
        :return: ddt_datalist
        '''
        ddt_datalist=[]
        nrows=self.readexcel.get_nrows(sheetname)
        for i in range(1,nrows):
            param_dict=self._get_param_dict(sheetname,i)
            ddt_datalist.append(param_dict)
        return ddt_datalist

    def _read_sheet(self,sheetname):
        '''
        读取指定sheet中的每行的数据信息并保存在字典中
        :param sheetname: sheet名称
        :return: 返回一个装有对应sheet的所有参数数据字典
        '''
        data_dict={}
        #获取指定sheet的行数
        getrows=self.readexcel.get_nrows(sheetname)
        #获取每行的值
        for i in range(1,getrows):
            get_data=self.readexcel.get_row_values(sheetname,i)     #获取当行
            data_dict[i]=get_data                                   #行数:参数值
        return data_dict

    def _get_param_dict(self,sheetname,key):
        '''
        获取data_dict中指定key对应的参数
        :param data_dict: 数据字典
        :param key: key值(1、2、3...)
        :return: 参数字典
        '''
        param_dict={}
        data_dict=self._read_sheet(sheetname)
        param_list=data_dict[key]
        #获取token值
        sql='select token from userlogin where telnum=17318971827;'
        token=self.db.get_selectdata_row(sql,0)
        #获取参数组合成字典
        param_dict['test_class']=param_list[0]
        param_dict['test_title']=param_list[1]
        param_dict['api']=param_list[2]
        param_dict['request_type']=param_list[3]
        param_dict['post_type']=param_list[4]
        param_dict['url_param']=param_list[5]
        param_dict['data_param']=param_list[6]
        param_dict['headers']=param_list[7]
        param_dict['errorCode']=param_list[8]
        param_dict['isTrue']=param_list[9]
        param_dict['errorMessage']=param_list[10]
        param_dict['token']=token[0]
        return param_dict

    def send_request(self,param_dict):
        '''
        根据参数字典发送请求
        :param param_dict : 参数字典
        :param worksheet : 工作sheet
        '''
        #获取参数字典各类参数值
        test_class=param_dict['test_class']
        test_title=param_dict['test_title']
        api=param_dict['api']
        request_type=param_dict['request_type']
        post_type=param_dict['post_type']
        url_param=param_dict['url_param']
        data_param=param_dict['data_param']
        headers=param_dict['headers']
        errorCode=param_dict['errorCode']
        isTrue=param_dict['isTrue']
        errorMessage=param_dict['errorMessage']

        #转换url_param参数
        if url_param=='':
            url_param=None
        elif '=' in url_param:
            url_param=url_param
        else:
            url_param=eval(url_param)
        #转换data_param参数
        if data_param=='':
            data_param=None
        else:
            data_param=eval(data_param)
        #转换errorCode'
        errorCode=int(errorCode)
        #转换isTrue参数
        if isTrue == 'True':
            isTrue = True
        elif isTrue == 'False':
            isTrue = False

        #判断发送类型并调用请求函数
        #get请求
        if request_type=='get':
            #发送请求
            try:
                result=self.req.get_method(api,test_title,url_param)
                #返回数据判断
                assert result['errorCode']==errorCode
                assert result['isTrue']==isTrue
                assert result['errorMessage']==errorMessage
                self.log.info('[%s]-[Result]:PASS'%test_title)
                self.log.info('-'*60)
            except Exception:
                self.log.error('[%s]-[Result]:FAILED' % test_title)
                self.log.info('-' * 60)
                raise
        #put请求
        elif request_type=='put':
            #发送请求
            try:
                result=self.req.put_method(api,test_title,data_param,url_param)
                # 返回数据判断
                assert result['errorCode'] == errorCode
                assert result['isTrue'] == isTrue
                assert result['errorMessage'] == errorMessage
                self.log.info('[%s]-[Result]:PASS' % test_title)
                self.log.info('-' * 60)
            except Exception:
                self.log.error('[%s]-[Result]:FAILED' % test_title)
                self.log.info('-' * 60)
                raise
        #delete请求
        elif request_type=='delete':
            #发送请求
            try:
                result=self.req.delete_method(api,test_title,url_param)
                # 返回数据判断
                assert result['errorCode'] == errorCode
                assert result['isTrue'] == isTrue
                assert result['errorMessage'] == errorMessage
                self.log.info('[%s]-[Result]:PASS' % test_title)
                self.log.info('-' * 60)
            except Exception:
                self.log.error('[%s]-[Result]:FAILED' % test_title)
                self.log.info('-' * 60)
                raise
        #post请求
        elif request_type=='post':
            # post类型为urlencode
            if post_type=='urlencode':
                try:
                    result=self.req.post_method('urlencode',api,test_title,data_param,url_param)
                    #返回数据判断
                    assert result['errorCode'] == errorCode
                    assert result['isTrue'] == isTrue
                    assert result['errorMessage'] == errorMessage
                    self.log.info('[%s]-[Result]:PASS' % test_title)
                    self.log.info('-' * 60)
                except Exception:
                    self.log.error('[%s]-[Result]:FAILED' % test_title)
                    self.log.info('-' * 60)
                    raise
            # post类型为form
            elif post_type=='form':
                try:
                    result=self.req.post_method('form',api,test_title,data_param,url_param)
                    # 返回数据判断
                    assert result['errorCode'] == errorCode
                    assert result['isTrue'] == isTrue
                    assert result['errorMessage'] == errorMessage
                    self.log.info('[%s]-[Result]:PASS' % test_title)
                    self.log.info('-' * 60)
                except Exception:
                    self.log.error('[%s]-[Result]:FAILED' % test_title)
                    self.log.info('-' * 60)
                    raise
            # post类型为json
            elif post_type=='json':
                try:
                    result=self.req.post_method('json',api,test_title,data_param,url_param)
                    # 返回数据判断
                    assert result['errorCode'] == errorCode
                    assert result['isTrue'] == isTrue
                    assert result['errorMessage'] == errorMessage
                    self.log.info('[%s]-[Result]:PASS' % test_title)
                    self.log.info('-' * 60)
                except Exception:
                    self.log.error('[%s]-[Result]:FAILED' % test_title)
                    self.log.info('-' * 60)
                    raise
            else:
                print('POST请求类型设置错误!')
Пример #8
0
class API_ZC(unittest.TestCase):
    '''注册类api接口测试'''
    @classmethod
    def setUpClass(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[ZC_api_start]<<<<<<<<<<<<<<<<<<<<')

    @classmethod
    def tearDownClass(self):
        self.db.cur_close()  #关闭游标
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[ZC_api_end]<<<<<<<<<<<<<<<<<<<<<<')

    def test_001_getfzcid(self):
        '''获取非注册用户标识id'''
        casename = '获取非注册用户标识id'
        sql1 = 'select * from userregister;'
        phoneid = self.db.get_specific_data(sql1, 0, 6)  #从数据库中获取手机号码数据
        api = self.yaml.get_ZCapi(0)
        data = {'phoneid': phoneid}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库fzcuid字段
            sql2 = 'update userregister set fzcuid=\'%s\' where phoneid=\'%s\'' % (
                result['result'], phoneid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_002_getverifycode(self):
        '''获取验证码'''
        casename = '获取验证码'
        api = self.yaml.get_ZCapi(2)
        sql1 = 'select * from verifycode;'
        telnum = self.db.get_specific_data(sql1, 0, 0)  #从数据库中获取手机号数据
        data = {'mobile': telnum, 'isregister': 1}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库verifycode字段
            sql2 = 'update verifycode set verifycode=\'%s\' where telnum=\'%s\'' % (
                result['result'], telnum)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_003_register(self):
        '''正式注册'''
        casename = '正式注册'
        api = self.yaml.get_ZCapi(1)
        sql1 = 'select * from userregister;'
        sql2 = 'select * from verifycode;'
        #获取数据信息--tuple
        sql1data = self.db.get_selectdata_row(sql1, 0)
        sql2data = self.db.get_selectdata_row(sql2, 0)
        #获取数据库数据信息
        username = sql1data[1]
        password = sql1data[3]
        fzcuid = sql1data[4]
        telnum = sql2data[0]
        verifycode = sql2data[1]
        data = {
            "userid": fzcuid,
            "username": username,
            "mobile": telnum,
            "password": password,
            "validcode": verifycode
        }

        # 发送请求
        try:
            result = self.req.post_method('json', api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            #更新数据库内容--注册领驭号
            sql3 = 'update userregister set zcuid=\'%s\' where telnum=\'%s\';' % (
                result['result'], telnum)
            sql4 = 'update userlogin set zcuid=\'%s\' where telnum=\'%s\';' % (
                result['result'], telnum)
            #更新用户注册状态
            sql5 = 'update userregister set zcbz=1 where telnum=\'%s\';' % telnum
            self.db.execute_sql(sql3)
            self.db.execute_sql(sql4)
            self.db.execute_sql(sql5)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            raise
Пример #9
0
class API_USER(unittest.TestCase):
    '''用户类api接口测试'''
    @classmethod
    def setUpClass(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.api = CommonApiTools()
        self.api.login_api_pwd()  #密码登陆
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[USER_api_start]<<<<<<<<<<<<<<<<<<<<')

    @classmethod
    def tearDownClass(self):
        self.api.logout_api()  #注销退出
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[USER_api_end]<<<<<<<<<<<<<<<<<<<<')

    def test_001_getuserinfo_byuserid(self):
        '''用户类-获取用户公开信息(通过userid)'''
        casename = '获取用户公开信息-通过userid'
        api = self.yaml.get_USERapi(0)
        sql = 'select * from userlogin;'
        #获取数据库信息
        sqldata = self.db.get_selectdata_row(sql, 0)
        userid = sqldata[3]
        mobile = sqldata[0]
        data = {'userid': userid}
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['result']['mobile'], mobile)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_002_getuserinfo_bymobile(self):
        '''用户类-获取用户公开信息(通过mobile)'''
        casename = '获取用户公开信息-通过mobile'
        api = self.yaml.get_USERapi(0)
        sql = 'select * from userlogin;'
        sqldata = self.db.get_selectdata_row(sql, 0)
        userid = sqldata[3]
        mobile = sqldata[0]
        data = {'mobile': mobile}
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['result']['userid'], userid)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_003_getuserinfo_byall(self):
        '''用户类-获取用户公开信息(通过userid和mobile)'''
        casename = '获取用户公开信息-通过userid/mobile'
        api = self.yaml.get_USERapi(0)
        sql = 'select * from userlogin;'
        sqldata = self.db.get_selectdata_row(sql, 0)
        userid = sqldata[3]
        mobile = sqldata[0]
        data = {'userid': userid, 'mobile': mobile}
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['result']['mobile'], mobile)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_004_updateinfo(self):
        '''用户类-更新用户信息'''
        casename = '更新用户信息'
        api = self.yaml.get_USERapi(2)
        sql1 = 'select * from userlogin;'
        sql1data = self.db.get_selectdata_row(sql1, 0)
        #获取数据信息
        userid = sql1data[3]
        mobile = sql1data[0]
        username = sql1data[8]
        city = 'ChengDu'
        phoneid = sql1data[4]
        jpushregisterid = 'lzl198903017'
        password = sql1data[1]
        gendor = random.randint(0, 3)
        introduction = 'Hello!I am bear,I am doing api autotest!'
        body = {
            'userid': userid,
            'mobile': mobile,
            'username': username,
            'city': city,
            'phoneid': phoneid,
            'jpushregisterid': jpushregisterid,
            'password': password,
            'gendor': gendor,
            'introduction': introduction
        }
        try:
            result = self.req.put_method(api, casename, body)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], 0)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库--userinfo
            sql2='update userinfo set userid=\'%s\',username=\'%s\',mobile=\'%s\',city=\'%s\',phoneid=\'%s\',' \
                 'jpushregisterid=\'%s\',password=\'%s\',gendor=\'%d\',introduction=\'%s\' where infoid=1;'\
                 %(userid,username,mobile,city,phoneid,jpushregisterid,password,gendor,introduction)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_005_uploadcontacts(self):
        '''用户类-上传通讯录'''
        casename = '上传通讯录'
        api = self.yaml.get_USERapi(5)
        sql = 'select * from userlogin;'
        #配置数据
        token = self.db.get_specific_data(sql, 0, 6)
        contactslist = [{
            'mobile': '13828836568',
            'name': 'feifei'
        }, {
            'mobile': '13921342314',
            'name': 'nannan'
        }]
        contactsjson = json.dumps(contactslist)
        data = 'token=%s' % token
        try:
            result = self.req.post_method('urlencode', api, casename, data,
                                          contactsjson)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_006_getcontacts(self):
        '''用户类-获取用户通讯录'''
        casename = '获取用户通讯录'
        api = self.yaml.get_USERapi(6)
        sql = 'select * from userlogin;'
        #配置数据
        token = self.db.get_specific_data(sql, 0, 6)
        data = {'token': token}
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s-[Result]:FAILED]' % casename)
            self.log.info('-' * 60)
            raise

    def test_007_finduser(self):
        '''用户类-查找嘟嘟用户'''
        casename = '查找嘟嘟用户'
        api = self.yaml.get_USERapi(1)
        sql = 'select * from userlogin;'
        #配置数据
        token = self.db.get_specific_data(sql, 0, 6)
        key = '王'
        data = {'token': token, 'key': key}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_008_updateintroduction(self):
        '''用户类-更新签名信息'''
        casename = '更新签名信息'
        api = self.yaml.get_USERapi(3)
        sql1 = 'select * from userlogin;'
        #获取数据信息
        token = self.db.get_specific_data(sql1, 0, 6)
        introduction = '好好学习,天天向上!'
        data = 'token=%s&introduction=%s' % (token, introduction)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_009_bindbluetooth(self):
        '''用户类-更新绑定蓝牙'''
        casename = '更新绑定蓝牙'
        api = self.yaml.get_USERapi(7)
        sql = 'select * from userlogin;'
        #获取数据信息
        token = self.db.get_specific_data(sql, 0, 6)
        bluetooth = '5065832AD0A9'
        data = 'token=%s&bluetooth=%s' % (token, bluetooth)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_010_unbindbluetooth(self):
        '''用户类-取消绑定蓝牙'''
        casename = '取消绑定蓝牙'
        api = self.yaml.get_USERapi(7)
        sql = 'select * from userlogin;'
        #获取数据信息
        token = self.db.get_specific_data(sql, 0, 6)
        bluetooth = ''
        data = 'token=%s&bluetooth=%s' % (token, bluetooth)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            raise

    def test_011_updateoftenplace(self):
        '''用户类-更新我的导航收藏'''
        casename = '更新我的导航收藏'
        api = self.yaml.get_USERapi(8)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        bodylist = [{
            "title": "公司",
            "address": "月光流域",
            "datatype": "history",
            "longitude": "123.422",
            "latitude": "22.2322",
            "updatetime": 2122212122
        }, {
            "title": "家",
            "address": "华侨城",
            "datatype": "favorite",
            "longitude": "123.4",
            "latitude": "22.23",
            "updatetime": 21212122
        }]
        data = 'userid=%s' % userid
        # 发送请求
        try:
            result = self.req.put_method(api, casename, bodylist, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_012_getoftenplace(self):
        '''用户类-获取我的导航收藏'''
        casename = '获取我的导航收藏'
        api = self.yaml.get_USERapi(9)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        data = {'userid': userid}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_013_updatechatrecord(self):
        '''用户类-更新会话记录'''
        casename = '更新会话记录'
        api = self.yaml.get_USERapi(10)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        bodylist = [{
            "type": "channel",
            "objid": "10012",
            "objname": "中华大帝国频道",
            "logourl":
            "http://120.76.194.47:8888/group1/M00/00/05/eEzCL1haOg_Ew5gSAACvGaRuld0687.png",
            "chattime": 21222121
        }]
        data = 'userid=%s' % userid
        # 发送请求
        try:
            result = self.req.put_method(api,
                                         casename,
                                         body=bodylist,
                                         data=data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_014_getchatrecord(self):
        '''用户类-获取会话记录'''
        casename = '获取会话记录'
        api = self.yaml.get_USERapi(11)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        data = {'userid': userid}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_015_chatrecord_add(self):
        '''用户类-更新会话增量模式--新增会话记录'''
        casename = '更新会话增量模式--新增会话记录'
        api = self.yaml.get_USERapi(12)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        mode = 'i'
        bodylist = [{
            "type": "channel",
            "objid": "10012",
            "objname": "中华大帝国频道",
            "logourl":
            "http://120.76.194.47:8888/group1/M00/00/05/eEzCL1haOg_Ew5gSAACvGaRuld0687.png",
            "chattime": 21222121
        }]
        #配置参数
        data = 'userid=%s&mode=%s' % (userid, mode)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, bodylist, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_016_chatrecord_update(self):
        '''用户类-更新会话增量模式--更新会话记录'''
        casename = '更新会话增量模式--更新会话记录'
        api = self.yaml.get_USERapi(12)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        mode = 'u'
        bodylist = [{
            "type": "channel",
            "objid": "10012",
            "objname": "中华大帝国频道",
            "logourl":
            "http://120.76.194.47:8888/group1/M00/00/05/eEzCL1haOg_Ew5gSAACvGaRuld0687.png",
            "chattime": 21222233
        }]
        #配置参数
        data = 'userid=%s&mode=%s' % (userid, mode)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, bodylist, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_017_chatrecord_delete(self):
        '''用户类-更新会话增量模式--删除会话记录'''
        casename = '更新会话增量模式--删除会话记录'
        api = self.yaml.get_USERapi(12)
        sql = 'select * from userlogin;'
        #获取数据信息
        userid = self.db.get_specific_data(sql, 0, 3)
        mode = 'd'
        bodylist = [{
            "type": "channel",
            "objid": "10012",
            "objname": "中华大帝国频道",
            "logourl":
            "http://120.76.194.47:8888/group1/M00/00/05/eEzCL1haOg_Ew5gSAACvGaRuld0687.png",
            "chattime": 21222233
        }]
        #配置参数
        data = 'userid=%s&mode=%s' % (userid, mode)
        # 发送请求
        try:
            result = self.req.put_method(api, casename, bodylist, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            raise
Пример #10
0
class API_DL(unittest.TestCase):
    '''登录类api接口测试'''
    def setUp(self):
        self.log = LogTools()
        self.yaml = YamlTools()
        self.req = RequestTools()
        self.db = DataBaseTools()
        self.api = CommonApiTools()

    def tearDown(self):
        self.db.cur_close()  # 关闭游标
        self.api.logout_api()  # 退出登陆

    def test_001_loginbypassword_uid(self):
        '''用户登录类-密码方式登陆认证(通过uid以及password进行登陆)'''
        self.log.info(
            '>>>>>>>>>>>>>>>>>>>>>[DL_api_start]<<<<<<<<<<<<<<<<<<<<')
        casename = '密码方式登陆认证_uid认证'
        api = self.yaml.get_DLapi(0)
        sql1 = 'select * from userlogin;'
        sql1data = self.db.get_selectdata_row(sql1, 0)
        #获取数据库数据
        zcuid = sql1data[2]
        password = sql1data[1]
        phoneid = sql1data[4]
        data = {'userid': zcuid, 'pwd': password, 'phoneid': phoneid}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库信息--用户领驭号、token
            sql2='update userlogin set dlbz=1,userid=\'%s\',token=\'%s\' where phoneid=\'%s\';'\
                 %(result['result']['userid'],result['result']['token'],phoneid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_002_loginbypassword_mobile(self):
        '''用户登录类-密码方式登陆认证(通过mobile以及password进行登陆)'''
        casename = '密码方式登陆认证_mobile认证'
        api = self.yaml.get_DLapi(0)
        sql1 = 'select * from userlogin;'
        sql1data = self.db.get_selectdata_row(sql1, 0)
        #获取数据库数据
        mobile = sql1data[0]
        password = sql1data[1]
        phoneid = sql1data[4]
        data = {'mobile': mobile, 'pwd': password, 'phoneid': phoneid}

        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库信息--用户领驭号、token
            sql2 = 'update userlogin set dlbz=1,userid=\'%s\',token=\'%s\' where phoneid=\'%s\';' \
               % (result['result']['userid'], result['result']['token'], phoneid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_003_loginbypassword_uid_mobile(self):
        '''用户登录类-密码方式登陆认证(通过uid、mobile以及password进行登陆)'''
        casename = '密码方式登陆认证_uid/mobile认证'
        api = self.yaml.get_DLapi(0)
        sql1 = 'select * from userlogin;'
        sql1data = self.db.get_selectdata_row(sql1, 0)
        #获取数据库数据
        zcuid = sql1data[2]
        mobile = sql1data[0]
        password = sql1data[1]
        phoneid = sql1data[4]
        data = {
            'userid': zcuid,
            'mobile': mobile,
            'pwd': password,
            'phoneid': phoneid
        }
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            #更新数据库信息--用户领驭号、token
            sql2 = 'update userlogin set dlbz=1,userid=\'%s\',token=\'%s\' where phoneid=\'%s\';' \
               % (result['result']['userid'], result['result']['token'], phoneid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_004_loginbyverifycode(self):
        '''用户登录类-验证码方式登陆认证'''
        casename = '验证码方式登陆认证'
        api = self.yaml.get_DLapi(1)
        #获取登陆验证码
        self.api.get_verifycode(0)
        #获取数据库数据
        sql1 = 'select * from userlogin;'
        sql1data = self.db.get_selectdata_row(sql1, 0)
        telnum = sql1data[0]
        loginverifycode = sql1data[7]
        phoneid = sql1data[4]
        data = {
            'mobile': telnum,
            'validcode': loginverifycode,
            'phoneid': phoneid
        }

        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True)
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
            # 更新数据库信息--用户领驭号、token、登陆标志
            sql2 = 'update userlogin set dlbz=1,userid=\'%s\',token=\'%s\' where phoneid=\'%s\';' \
                   % (result['result']['userid'], result['result']['token'], phoneid)
            self.db.execute_sql(sql2)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_005_checkmobile(self):
        '''用户登录类-检查手机号码是否注册(手机号码已经注册)'''
        casename = '检查手机号码是否注册-已经注册'
        sql = 'select * from userlogin;'
        #获取数据
        telnum = self.db.get_specific_data(sql, 0, 0)
        api = self.yaml.get_DLapi(2)
        data = {'mobile': telnum}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 0) and self.assertEqual(
                result['isTrue'], True) and self.assertEqual(
                    result['errorMessage'], '该手机号码已经注册!')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info('-' * 60)
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info('-' * 60)
            raise

    def test_006_checkmoblie(self):
        '''用户登录类-检查手机号码是否注册(手机号码未注册)'''
        casename = '检查手机号码是否注册-未注册'
        sql = 'select * from userlogin;'
        #获取数据
        telnum = self.db.get_specific_data(sql, 1, 0)
        api = self.yaml.get_DLapi(2)
        data = {'mobile': telnum}
        # 发送请求
        try:
            result = self.req.get_method(api, casename, data)
            self.assertEqual(result['errorCode'], 10351) and self.assertEqual(
                result['isTrue'], False) and self.assertEqual(
                    result['errorMessage'], '该手机未注册')
            self.log.info('[%s]-[Result]:PASS' % casename)
            self.log.info(
                '>>>>>>>>>>>>>>>>>>>>>[DL_api_end]<<<<<<<<<<<<<<<<<<<<<<')
        except Exception:
            self.log.error('[%s]-[Result]:FAILED' % casename)
            self.log.info(
                '>>>>>>>>>>>>>>>>>>>>>[DL_api_end]<<<<<<<<<<<<<<<<<<<<<<')
            raise