# -*- coding:utf-8 -*-

'''
@project: ApiAutoTest
@author: Jimmy
@file: test_register.py
@ide: PyCharm Community Edition
@time: 2018-12-28 15:50
@blog: https://www.cnblogs.com/gotesting/ ''' import unittest
import os
from ddt import ddt,data
from Common.http_request import HttpRequest
from Common.read_excel import ReadExcel
from Common.read_config import ReadConfig
from Common.mysql_util import MysqlUtil
from Common.get_logger import GetLog
from Common.basic_data import DoRegex,Context
from Common.contants import *
import json
import re # 读取配置文件,获取当前URL前缀,用于灵活更换测试服务器地址
read_config = ReadConfig()
url_pre = read_config.get_config_str('api','url_pre') # 读取excel,获取login测试数据
data_dir = os.path.join(data_dir,'test_data.xlsx')
read_excel = ReadExcel(data_dir)
register_cases = read_excel.get_cases('register') get_log = GetLog() @ddt
class TestRegister(unittest.TestCase): @classmethod
def setUpClass(cls):
global max_mobilephone
mysql = MysqlUtil()
sql = 'SELECT MobilePhone FROM future.member WHERE MobilePhone != "" ORDER BY MobilePhone DESC LIMIT 1;'
sql_resp = mysql.fetch_one(sql)
if sql_resp is not None:
max_mobilephone = sql_resp['MobilePhone']
get_log.log_info(' the max_mobilephone from mysql is {0}'.format(max_mobilephone))
else:
max_mobilephone = 15964506666
get_log.log_info(' the max_mobilephone from mysql is None , we use {0}'.format(max_mobilephone)) @classmethod
def tearDownClass(cls):
pass @data(*register_cases)
def test_resister(self,case):
url = url_pre + case.url # 使用正则表达式 匹配 读取的case.data中的mobilephone,匹配成功后,将查询数据库获取的最大mobilephone+1 赋值给 case.data中的mobilephone,以防注册手机号重复
data = json.loads(case.data)
register_user = int(max_mobilephone) + 1
if re.findall(pattern='\$\{(.*?)\}',string=data['mobilephone']):
data['mobilephone'] = register_user # 记录当前测试case信息
get_log.log_info('''Test Case Info:
case_id : {0}
title : {1}
method : {2}
url : {3}
data : {4}
expected: {5}
'''.format(case.case_id,case.title,case.method,url,data,case.expected)) # 注册接口请求,获取响应
response = HttpRequest(method=case.method,url=url,data=data)
actual = response.get_json()['msg'] # 记录当前测试case 接口响应信息
get_log.log_info('''Test Case Request Response Result:
response : {0}
actual : {1}
'''.format(response.get_json(),actual)) # 接口请求实际结果与期望结果做校验
try:
self.assertEquals(case.expected,actual)
read_excel.write_result('register',case.case_id,actual,'Pass')
get_log.log_info('Test Result is Passed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
except Exception as e:
read_excel.write_result('register',case.case_id,actual,'Fail')
get_log.log_info('Test Result is Failed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
get_log.log_error('Error msg :{0}'.format(e))
raise e # 数据库校验
if actual == '注册成功' or actual == '手机号码已被注册':
sql_verify = 'SELECT MobilePhone FROM future.member WHERE MobilePhone = {0};'.format(data['mobilephone'])
sql_result = MysqlUtil().fetch_one(sql_verify)
if sql_result is not None:
self.assertEquals(str(data['mobilephone']),sql_result['MobilePhone'])
get_log.log_info('Test Result is Passed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
else:
get_log.log_info('Test Result is Failed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
get_log.log_error('Error msg :{0}'.format(AssertionError))
raise AssertionError
else:
sql_verify = 'SELECT MobilePhone FROM future.member WHERE MobilePhone = {0};'.format(data['mobilephone'])
sql_result = MysqlUtil().fetch_one(sql_verify)
print(sql_result)
try:
self.assertEquals(None,sql_result)
get_log.log_info('Test Result is Passed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
except Exception as e:
get_log.log_info('Test Result is Failed ! case_id is {0},title is {1} '.format(case.case_id,case.title))
get_log.log_error('Error msg :{0}'.format(e))
raise e

python - 接口自动化测试 - TestRegister - 注册接口测试用例的更多相关文章

  1. 使用python进行接口自动化测试,批量执行测试用例

    工作中,使用python的requests库进行接口自动化测试是一个比较不错的选择,今天就以某网站的免费接口为例,展示以get请求进行批量执行测试用例.话不多说直接开讲 分析一下接口信息, 请求地址: ...

  2. python - 接口自动化测试 - TestRecharge - 充值接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_recharge.py @ide: PyChar ...

  3. python - 接口自动化测试 - TestLogin - 登录接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...

  4. python web自动化测试框架搭建(功能&接口)——测试用例执行和结果收集

    由于unittest框架中结果收集在不同文件中,所以此处重写结果收集方法,加入执行时间,失败信息,失败截图等 TestRunner.py # coding=utf-8 import sys impor ...

  5. python web自动化测试框架搭建(功能&接口)——接口用例实现

    测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...

  6. python web自动化测试框架搭建(功能&接口)——接口公共方法

    接口公共方法有:数据引擎.http引擎.Excel引擎 1.数据引擎:获取用例.结果检查.结果统计 # -*- coding:utf-8 -*- from XlsEngine import XlsEn ...

  7. python web自动化测试框架搭建(功能&接口)——接口测试模块

    Python接口测试采用python读取excel的方法,通过requests库发送请求和接收响应.模块有: Data:用于存放excel用例的,用例格式: iutil: 接口公共方法,数据引擎.ht ...

  8. python web自动化测试框架搭建(功能&接口)——功能测试模块

    功能测试使用selenium,模块有: 1.futil: 公共方法,如元素高亮显示 # coding=utf-8 """高亮显示元素""" ...

  9. python web自动化测试框架搭建(功能&接口)——通用模块

    1.通用模块: config.conf: 公共配置文件,配置报告.日志.截图路径,以及邮件相关配置 [report] reportpath = E:\workspace\WebAutomation\s ...

随机推荐

  1. 自己实现的简单的grid

    12年在第一家公司的时候,有过很长一段时间在前端的使用研究上.一开始的时候使用ExtJs4.0 MVC 来开发前端,觉得里面的风转的组件非常好用,Panel.window.tree等等,简化了对于前端 ...

  2. cms系统-帖子页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. 在 Windows下用 Visual Studio 编译 OpenSSL 1.1.0

    到OpenSSL官方网站下载OpenSSL源代码包 1.下载 openssl-1.1.0.tar.gz 2.安装 ActivePerl, 可以到http://www.activestate.com/a ...

  4. idea搭建ssm

    第一步:打开intellij idea,创建maven项目 参考:http://blog.csdn.net/w8897282/article/details/71173211  1.创建一个maven ...

  5. log4j.properties中的这句话“log4j.logger.org.hibernate.SQL=DEBUG ”该怎么写在log4j.xml里面呢?

    http://www.cnblogs.com/gredswsh/p/log4j_xml_properties.html 请问:log4j.properties中的这句话“log4j.logger.or ...

  6. xtarbackup恢复

    xbstream -x < ynhw-mysql-slave.01.mysql.prod.sg_fullbak_20180326134255.xbstream -C /data/mysql cd ...

  7. C++的XML编程经验――LIBXML2库使用指南

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  8. react的constructor和super的具体含义和使用

    1.constructor( )-----super( )的基本含义 这是ES6对类的默认方法,通过 new 命令生成对象实例时自动调用该方法.并且,该方法是类中必须有的,如果没有显示定义,则会默认添 ...

  9. c语言中--typeof--关键字用法

    C语言中 typeof 关键字是用来定义变量数据类型的.在linux内核源代码中广泛使用. 下面是Linux内核源代码中一个关于typeof实例: #define min(x, y) ({ \ typ ...

  10. kafka及扩展的安装笔记

    参考文件 https://blog.csdn.net/weiwenjuan0923/article/details/76152744 一.首先确认下jdk有没有安装 安装参照这个连接 https:// ...