接口自动化--数据库

接口测试可能会需要从数据库获取测试数据或者对测试结果进行数据校验,对日常使用的sql做一个简单的封装。

import pymysql

class MysqlHelper():
    def __init__(self,host,port,db,user,passwd,charset='utf8'):
        self.host=host
        self.port=port
        self.db=db
        self.user=user
        self.passwd=passwd
        self.charset=charset
        self.cursorclass = pymysql.cursors.DictCursor

    def connect(self):
        self.conn=pymysql.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset,cursorclass = pymysql.cursors.DictCursor)
        self.cursor=self.conn.cursor()

    def close(self):
        self.cursor.close()
        self.conn.close()

    def get_one(self,sql,params=()):
        result=None
        try:
            self.connect()
            self.cursor.execute(sql, params)
            result = self.cursor.fetchone()
            self.close()
        except Exception as e:
            print(e)
        return result

    def get_all(self,sql,params=()):
        list=()
        try:
            self.connect()
            self.cursor.execute(sql,params)
            list=self.cursor.fetchall()
            self.close()
        except Exception as e:
            print(e)
        return list

    def insert(self,sql,params=()):
        self.__edit(sql,params)

    def update(self, sql, params=()):
        self.__edit(sql, params)

    def delete(self, sql, params=()):
        self.__edit(sql, params)

    def create(self,sql,parames=()):
        self.__edit(sql, parames)

    def __edit(self,sql,params):
        response=None
        try:
            self.connect()
            response=self.cursor.execute(sql,params)
            self.conn.commit()
            self.close()
        except Exception as e:
            print(e)
        return response

你可能感兴趣的:(接口自动化测试框架,数据库,python,pymysql)