1.comm里放置数据操作代码
2.conftest.py里放置删除用户代码
3.test_register.py里放置测试注册用户的代码(运行注册接口之前先从数据库删除注册的账号)
----------------------------comm---------------------------------------
import pymysql
dbinfo={
"host":"192.16.8.x",
"post":3306,
"user":"root",
"password":"xxxx",
"database":"xxx"
}
class DBopera:
def __init__(self,dbinfo=dbinfo):
self.conn = pymysql.connect(**dbinfo)
self.cur = self.conn.cursor()
def dbselect(self,sql):
self.cur.execute(sql)
result = self.cur.fetchall()
print(result)
def dbcommit(self,sql):
self.cur.execute(sql)
self.conn.commit()
def dbclose(self):
self.cur.close()
self.conn.close()
-----------------------------------------------------------------------
------------------------------conftest.py------------------------------
import pytest
import pymysql
from comm import conn.DBopera
@pytest.fixture(params=["username1","username2"]
def delete_user(request):
user = request.param
db = DBopera()
db.dbcommit(sql='delete from tablename where username = "%s";'%user)
db.close()
return user
-------------------------test_register.py------------------------------
import requests
def test_register(delete_user):
url = "http://xxxx/register"
body = {
"username":delete_user,
"password":"123456",
"email":"[email protected]"
}
r = requests.post(url,json=body)
print(r.text())
标签:cur,python,self,sql,接口,user,conn,mysql,def From: https://www.cnblogs.com/wuhhaa/p/16600490.html