import pymysql
import pytest
conn = pymysql.connect(
user='root',
passwd='123456',
host='localhost',
port=3306,
db='my_test'
)
def test_db():
qs = 'select uid,uname,pwd from test_user'
lst = []
try:
cursor = conn.cursor()
cursor.execute(qs)
# 获取所有的数据
r = cursor.fetchall()
for x in r:
# x 表示列
u = (x[0],x[1],x[2])
lst.append(u)
return lst
finally:
cursor.close()
conn.close()
@pytest.mark.parametrize('uid,uname,pwd',test_db())
def test04(uid,uname,pwd):
print(uid,uname,pwd)
if __name__ == '__main__':
pytest.main(['-vs', 'test_file.py'])
控制台:
============================= test session starts =============================
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <pymysql.cursors.Cursor object at 0x000001FC4322A5C8>
query = 'select uid,uname,pwd from test_user', args = None
def execute(self, query, args=None):
"""Execute a query
:param str query: Query to execute.
:param args: parameters used with query. (optional)
:type args: tuple, list or dict
:return: Number of affected rows
:rtype: int
If args is a list or tuple, %s can be used as a placeholder in the query.
If args is a dict, %(name)s can be used as a placeholder in the query.
"""
while self.nextset():
pass
query = self.mogrify(query, args)
> result = self._query(query)
.
def close(self):
"""
Send the quit message and close the socket.
See `Connection.close() <https://www.python.org/dev/peps/pep-0249/#Connection.close>`_
in the specification.
:raise Error: If the connection is already closed.
"""
if self._closed:
> raise err.Error("Already closed")
E pymysql.err.Error: Already closed
..\..\..\..\..\Anaconda3\lib\site-packages\pymysql\connections.py:356: Error
========================= 1 failed, 3 passed in 0.30s =========================
Process finished with exit code 1
PASSED [ 50%]1 tom 123
PASSED [ 75%]2 kite 123
PASSED [100%]3 rows 456
Assertion failed
Assertion failed
生成表时会申明字段类型和约束,传入时要注意顺序和类型以及传入数量
没传参,只是个查询,
哥 查询select 字段不是参数?字段名要对应