python 读取数据表数据(Dtt+Pytest)


 1 import pymysql
 2 import pytest
 3 
 4 
 5 def get_mysql_data():
 6     # 打开数据库连接
 7     db = pymysql.connect(host='127.0.0.1', database='test_db', port=3306, user='root', password='123456')
 8     # 获取数据库游标
 9     cursor = db.cursor()
10     # 执行sql语句
11     sql = "select name,age,six from user_info"
12     cursor.execute(sql)
13     # 返回所有查询结果
14     lst_data = cursor.fetchall()
15   cursor.close() # 关闭游标
16   db.close() # 关闭数据库连接
17     return lst_data
18 
19 
20 @pytest.mark.parametrize('name,age,six', get_mysql_data())
21 def test_mysql(name, age, six):
22     print(name, age, six)
23 
24 
25 if __name__ == '__main__':
26     # print(get_mysql_data())
27     pytest.main(['-vs', 'test_mysql.py'])