使用pytest分别从xlsx,json,csv及数据库中读取数据,以实现数据驱动

使用pytest分别从xlsx,json,csv中读取数据,以实现数据驱动

从csv中读取数据:

tom,kate,rose
zhangsan, lisi, wangwu
#-*- coding = utf-8 -*-
#@Time: 2021/4/15 17:22
#@Author : 
#@File : test_csv.py
#@Software : PyCharm

import pytest
import csv

def get_data():
    with open('test.csv') as f:
        lst = csv.reader(f)#lst应该是一个列表的列表,其中每个元素是一个列表,其中每个元素为同一行的元素
        my_data = []
        for row in lst:
            my_data.extend(row)
        return my_data
@pytest.mark.parametrize('name',get_data())
def test01(name):
    print(name)

if __name__ == '__main__':
    # print(get_data())
    pytest.main(['-sv' ,'test_csv.py'])

从json中读取数据:

{
  "keys": ["tom","kate","rose"]
}
#-*- coding = utf-8 -*-
#@Time: 2021/4/15 21:02
#@Author : 
#@File : test_json.py
#@Software : PyCharm

import pytest
import json

def get_data():
    with open('test.json') as f:
        lst = []
        data = json.load(f)
        lst.extend(data['keys'])
        return lst

@pytest.mark.parametrize('name',get_data())
def test01(name):
    print(name)

if __name__ == '__main__':
    # print(get_data())
    pytest.main(['-sv','test_json.py'])

从xlsx中读取数据
在这里插入图片描述

#-*- coding = utf-8 -*-
#@Time: 2021/4/15 21:11
#@Author : 
#@File : test_excel.py
#@Software : PyCharm
import pytest
import xlrd

def get_data():
    filename = 'data.xlsx'#在pycharm外面建立一个xlsx文件复制进来才可以的
    wb = xlrd.open_workbook(filename)
    sheet = wb.sheet_by_index(0)
    rows = sheet.nrows
    cols = sheet.ncols
    lst = []

    for row in range(rows):
        for col in range(cols):
            cell_data = sheet.cell_value(row, col)
            lst.append(cell_data)
    return lst

@pytest.mark.parametrize('name', get_data())
def test1(name):
    print(name)

if __name__ == '__main__':
    pytest.main(['-sv', 'test_excel.py'])

从数据库中读取数据

mysql> create database  testing_db;#创建数据库
mysql> use testing_db;#切换到数据库
mysql> create table user_tbl(   #创建数据表
    -> id int primary key auto_increment,
    -> username varchar(20),
    -> pwd varchar(20))
    -> ;
mysql> select * from user_tbl;
+----+----------+------+
| id | username | pwd  |
+----+----------+------+
|  1 | tom      | 123  |
|  2 | kate     | 456  |
|  3 | rose     | 789  |
+----+----------+------+
3 rows in set (0.00 sec)
#-*- coding = utf-8 -*-
#@Time: 2021/4/15 22:41
#@Author : 
#@File : test_db.py
#@Software : PyCharm

import MySQLdb
import pytest

conn = MySQLdb.connect(
    user = 'root',
    passwd = 'PASSWD',
    host = 'localhost',
    port = 3306,
    db = 'testing_db'
)

def get_data():
    query_sql = 'select id,username,pwd from user_tbl'
    lst = []
    try:
        cursor = conn.cursor()
        cursor.execute(query_sql)
        r = cursor.fetchall()
        for x in r:
            u = (x[0],x[1],x[2])
            lst.append(u)
        return lst
    finally:
        cursor.close()
        conn.close()
@pytest.mark.parametrize('id,name,pwd',get_data())
def test1(id, name, pwd):
    print(id, name, pwd)


if __name__ == '__main__':
    pytest.main(['-sv'])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑着的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值