1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python数据驱动+读取yaml文件+读取excel文件+mySQL

python数据驱动+读取yaml文件+读取excel文件+mySQL

时间:2020-04-03 06:38:14

相关推荐

python数据驱动+读取yaml文件+读取excel文件+mySQL

简介

1、安装

pip install ddt

2、使用

import unittest,ddtdata=[{"user":'admin',"pwd":22222,"key":"asd"},{"user":'admin2',"pwd":2132123,"key":"tretr"},{"user":'admin3',"pwd":232243,"key":"dasd"}]@ddt.ddt()class Test_ddt(unittest.TestCase):@ddt.data(*data)def test_001(self,case):#遍历列表print(case['pwd'])if __name__ == '__main__':unittest.main()

即使是这样,也还没有做到真正的数据驱动。

yaml简介

1、yaml : Yet Another Markup Language :另一种标记语言。yaml 是专门用来写配置文件的语言,非常简洁和强大,更直观,更方便,有点类似于json格式。在自动化测试用的相当多,所以需要小伙伴们要熟练掌握,把测试数据存放在yaml文件,也可以进行参数化。

2、yaml基本语法规则:

大小写敏感使用缩进表示层级关系缩进时不允许使用Tab键,只允许使用空格。缩进的空格数目不重要,只要相同层级的元素左侧对齐即可'#'表示注释,从这个字符一直到行尾,都会被解析器忽略,这个和python的注释一样

3、yaml支持的数据结构有三种:

对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)纯量(scalars):单个的、不可再分的值。字符串、布尔值、整数、浮点数、Null、时间、日期

yaml的使用

1.使用pip安装pyyaml模块

pip install pyyaml

2.yaml的基本使用

在同一目录下新建 test_data.yaml 和 read_yaml.py文件。

yaml文件:

test_data.yaml

-phone : 1326557key : 01829bab25a57e4c11eff56db56d1f94except : 200

py文件:

read_yaml.py

import yamlf = open('test_data.yaml', 'r', encoding='utf-8')cfg = f.read()d = yaml.load(cfg, Loader=yaml.FullLoader) # 用load转字典# yaml5.1版本后弃用了yaml.load(file)这个用法,因为觉得很不安全,5.1版本之后就修改了需要指定Loader,通过默认加载器(FullLoader)禁止执行任意函数# Loader=yaml.FullLoader 加上这行代码,告警就没了print(d)

去掉告警的方法看以上备注:

运行结果:

[{'phone': 1326557, 'key': '01829bab25a57e4c11eff56db56d1f94', 'except': 200}]

注意:在书写yaml文件内容时,记得书写格式 ‘-’表示分隔一组数据,特别注意缩进和空格,不对的话会导致文件读取失败

3.封装yaml

以上是yaml文件的读取的简单用法,将她进行封装好,后续使用直接调用即可。

import yamlimport osclass GetYaml():def __init__(self, file_path):# 判断文件是否存在if os.path.exists(file_path):self.file_path = file_pathelse:print('没有找到%s文件路径' % file_path)self.data = self.read_yaml()def read_yaml(self):with open(self.file_path, 'r', encoding='utf-8')as f:p = f.read()return pdef get_data(self, key=None):result = yaml.load(self.data, Loader=yaml.FullLoader)# 判断key是否存在if key == None:return resultelse:return result.get(key)if __name__ == '__main__':read_yaml = GetYaml('test_data.yaml')#实例化,并且传递yaml文件的路径到类中r = read_yaml.get_data()print(r)

4.python常用数据类型在yaml中的书写格式

做接口自动化或者UI自动化的时候,要进行数据分离,把测试数据写在yaml文件中,即在yaml文件中写测试用例。

把常用的格式列出来:

1.yaml是键值对的格式,python中字典也是键值对的形式。

yaml文件的格式:

user: adminpsw: 123456

python3.7读取后的格式:

{'user': 'admin', 'psw': 123456}

2.字典嵌套字典

yaml文件的格式:

input:user: adminpsw: 123456

python3.7读取后的格式:

{'input': {'user': 'admin', 'psw': 123456}}

3.列表中嵌套字典(list)

yaml里面写一个数组,前面加一个’-'符号.

yaml文件的格式:

- test1: 123456- test2: 111111- test3: 222222

python3.7读取后的格式:

[{'test1': 123456}, {'test2': 111111}, {'test3': 222222}]

4.list嵌套dict

yaml文件格式:

- user: admin1pwd: '123456'- user: admin2pwd: '123321'- user: admin3pwd: '123457'

python3.7读取后的格式:

[{'user': 'admin1', 'pwd': '123456'}, {'user': 'admin2', 'pwd': '123321'}, {'user': 'admin3', 'pwd': '123457'}]

5.dict嵌套list

yaml文件中的格式:

input1:- admin1- '123456'input2:- admin2- '123321'input3:- admin3- '123457'

python3.7读取后的格式:

{'input1': ['admin1', '123456'], 'input2': ['admin2', '123321'], 'input3': ['admin3', '123457']}

excel数据读取

在cmd窗口安装:

pip install xlrd

基本操作

封装excel操作

excel结合ddt

mySQL封装操作:

# -*- coding:utf-8 -*-# @Time:/3/3 13:21# @Author:whweia# @File:MySQL_dome.pyimport pymysql# host='47.113.116.171',# port=3306,# user='root',# password='root',# db='recruit_students'class MysqlManager(object):def __init__(self, host, port, user, password, db):self.host = hostself.port = portself.user = userself.password = passwordself.db = dbdef connect(self):"连接数据库"try:conn = pymysql.connect(host=self.host,port=self.port,user=self.user,password=self.password,db=self.db)cursor = conn.cursor()return cursorexcept Exception as e:print(e)def get_data(self, sql):try:cursor = self.connect()cursor.execute(sql)return cursor.fetchone()except:print('数据库操作失败,请检查!')finally:cursor.close()def delete_data(self, param):try:conn = self.connect()cursor = conn.cursor()sql = 'DELETE from t_school_info WHERE f_school_name = "%s"' % paramprint(sql)cursor.execute(sql)result = mit()return resultexcept:print("操作失败,请检查!")# finally:#cursor.close()#conn.close()if __name__ == "__main__":host='192.168.1.15',port=3306,user='root',password='',db='student'mc = MysqlManager(host, port, user, password, db)r = mc.delete_data("573097")print(r)# r = mc.delete_data(6389048)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。