接口自动化编码-python
代码
利用了requests库去负责请求响应,利用unitest做结果判断,利用htmlreportrunner生成报告。
项目的结构如下:
Reports目录是生成报告的存储目录
Testcases存放测试用例
Testtools是测试相关的封装,比如里面我封装了pymysql的调用
点击run.py执行运行并自动生成报告。
直接跳到测试用例看下实现的用例代码:
import unittestimport requests import re from testtools.MySQLHelper import SqlHelper class readnote(unittest.TestCase): mUrl = 'http://127.0.0.1:8000/note/readnotes'; def setUp(self): pass def tearDown(self): pass def test_readnote_null(self): '''所有参数为空''' resp = requests.get(self.mUrl).json(); self.assertTrue(resp['code'] == 0); self.assertTrue(resp['data'] != []); r = SqlHelper().query('SELECT * FROM account_note WHERE public=1'); self.assertTrue(len(r) == len(resp['data'])); def test_readnote_only_token(self): '''只填写token,获取自己的列表''' resp = requests.get(self.mUrl,headers={'TOKEN':'1'}).json(); self.assertTrue(resp['code'] == 0); self.assertTrue(resp['data'] != []); r = SqlHelper().query("SELECT * FROM account_user a JOIN account_note b ON a.uid = b.uuid WHERE TOKEN='1'"); self.assertTrue(len(r) == len(resp['data'])); def test_readnote_only_uid(self): '''只填写uid,获取用户列表''' pass def test_readnote_all(self): '''参数都填写''' pass def test_readnote_invalidataUID(self): '''uid不存在''' pass def test_readnote_invalidataToken(self): '''token不存在''' pass def test_readnote_no_content(self): '''新注册用户,无内容返回''' pass
通过request请求获取数据后,解析json数据,首先判断code和data,然后解析data看下具体业务返回得对不对,作为断言的判断,是根据sql查询数据库的数据对比得到的。后面的断言,jmeter要实现起来会比较繁琐,更别说维护了。
mysql辅助类,自己封装的:
#--encoding:utf-8-- # import MySQLdb class MySQLHelper: myVersion=0.1 def __init__(self,host,user,password,charset="utf8"): self.host=host self.user=user self.password=password self.charset=charset try: self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.password) self.conn.set_character_set(self.charset) self.cursor=self.conn.cursor() except MySQLdb.Error as e: print ('MySql Error : %d %s' %(e.args[0],e.args[1])) def setDB(self,db): try: self.conn.select_db(db) except MySQLdb.Error as e: print ('MySql Error : %d %s' %(e.args[0],e.args[1])) def query(self,sql): try: rows=self.cursor.execute(sql) return rows; except MySQLdb.Error as e: print('MySql Error: %s SQL: %s'%(e,sql)) def queryOnlyRow(self,sql): try: self.query(sql) result=self.cursor.fetchone() desc=self.cursor.description row={} for i in range(0,len(result)): row[desc[i][0]]=result[i] return row; except MySQLdb.Error as e: print('MySql Error: %s SQL: %s'%(e,sql)) def queryAll(self,sql): try: self.query(sql) result=self.cursor.fetchall() desc=self.cursor.description rows=[] for cloumn in result: row={} for i in range(0,len(cloumn)): row[desc[i][0]]=cloumn[i] rows.append(row) return rows; except MySQLdb.Error as e: print('MySql Error: %s SQL: %s'%(e,sql)) def insert(self,tableName,pData): try: newData={} for key in pData: newData[key]="'"+pData[key]+"'" key=','.join(newData.keys()) value=','.join(newData.values()) sql="insert into "+tableName+"("+key+") values("+value+")" self.query("set names 'utf8'") self.query(sql) self.commit() except MySQLdb.Error as e: self.conn.rollback() print('MySql Error: %s %s'%(e.args[0],e.args[1])) finally: self.close() def update(self,tableName,pData,whereData): try: newData=[] keys=pData.keys() for i in keys: item="%s=%s"%(i,"'"+pData[i]+"'") newData.append(item) items=','.join(newData) newData2=[] keys=whereData.keys() for i in keys: item="%s=%s"%(i,"'"+whereData[i]+"'") newData2.append(item) whereItems=" AND ".join(newData2) sql="update "+tableName+" set "+items+" where "+whereItems self.query("set names 'utf8'") self.query(sql) self.commit() except MySQLdb.Error as e: self.conn.rollback() print('MySql Error: %s %s'%(e.args[0],e.args[1])) finally: self.close() def getLastInsertRowId(self): return self.cursor.lastrowid def getRowCount(self): return self.cursor.rowcount def commit(self): self.conn.commit() def close(self): self.cursor.close() self.conn.close()