数据驱动:在自动化测试中,需要把测试的数据分离到JSON、Yaml等文件中。

一、JSON

其中json文件中写代码必须是双引号

step1:新建一个名为“数据驱动”的包,再在这个包里新建一个文件名为“login.json”文件,再建一个python file,名叫“opertationJson.py”的文件,如下图所示:

python usb驱动开发 python写驱动程序_数据驱动

step2:在“login.json”文件中写入如下代码:

{
  "login": {"username": "Lucy","password": "17166230"}
}

假设我要读取:“17166230”

注意:冒号(:)后要空一格

step3:在“opertationJson.py”文件中写入如下代码:

1 import json
2 def readJson():
3     return json.load(open(file="login.json",mode="r",encoding="UTF-8"))
4 print(readJson(),type(readJson()))
5 print(readJson()["login"]["password"])

运行结果如下:

{'login': {'username': 'Lucy', 'password': '17166230'}} <class 'dict'>
17166230

Process finished with exit code 0

二、Yaml

Yaml:是一个可读性高,用来表达数据序列化的格式。

Yaml是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。

Yaml的配置文件后缀为.yml。如:login.yaml

Yaml对大小写很敏感,而且缩进表示层级关系,缩进不允许使用tab,只允许空格

Python操作Yaml文件需要单独安装第三方的库,安装的命令具体如下:

pip install pyyaml

step1:新建一个名为“数据驱动”的包,再在这个包里新建两个文件一个名为“login.yaml”,一个名为“data.Yaml”文件,再建一个python file,名叫“opertationYaml.py”的文件,如下图所示:

python usb驱动开发 python写驱动程序_python usb驱动开发_02

step2:在“login.yaml”文件中写入如下代码:

python usb驱动开发 python写驱动程序_List_03

注意:冒号(:)后要空一格,而且这种形式一般都是字典型。

假设我要读取:“黛西”和“黛西西小店”

“data.Yaml”文件中写入如下代码:

python usb驱动开发 python写驱动程序_json_04

注意:冒号(:)后要空一格,而且这种形式一般都是列表型。

假设我要读取:“黛西”和“Lucy”

step3:在“opertationYaml.py”文件中写入如下代码:

1 #假设我要读取:“黛西”和“黛西西小店”
 2 import yaml
 3 def readYaml():
 4     with open(file="login.yaml",mode="r",encoding="UTF-8") as f:
 5         return yaml.safe_load(f)
 6 print(readYaml(),type(readYaml()))
 7 print(readYaml()["login"]["username"])
 8 print(readYaml()["ali"]["taobao"]["shop"]["name"])
 9 
10 
11 #假设我要读取:“黛西”和“Lucy”
12 import yaml
13 def readYamlList():
14     with open(file="data.Yaml",mode="r",encoding="UTF-8") as f:
15         return list(yaml.safe_load_all(f))
16 print(readYamlList(),type(readYamlList()))
17 print(readYamlList()[0]["login"]["username"])
18 print(readYamlList()[1]["login"]["username"])

运行结果如下:

#假设我要读取“黛西”和“黛西西小店”,运行结果如下:
{'login': {'username': '黛西', 'password': 17166230}, 'ali': {'taobao': {'shop': {'name': '黛西西小店'}}}} <class 'dict'>
黛西
黛西西小店

Process finished with exit code 0



#假设我要读取“黛西”和“Lucy”,运行结果如下:
[{'login': {'username': '黛西'}}, {'login': {'username': 'Lucy'}}] <class 'list'>
黛西
Lucy

Process finished with exit code 0

 三、CSV

在Python中读取csv⽂件,直接使⽤标准库csv就可以的,在csv的库⾥⾯,读取⽂件的⽅式主要分为两种⽅式,字典或者是列表的⽅式来读取数据,下⾯分别展示这两种读取⽂件的⽅式:

3-1:列表

step1::新建一个名为“数据驱动”的包,再在这个包里新建一个文件名为“data.csv”,的文件,再建一个python file,名叫“csv学习.py”的文件,如下图所示:

python usb驱动开发 python写驱动程序_python usb驱动开发_05

step2:在“data.csv”文件中写入如下代码:

python usb驱动开发 python写驱动程序_json_06

假设我要读取:第二,三行的内容

step3:在“csv学习.py”文件中写入如下代码:

1 import csv
 2 def readCsvList():
 3     lists=[]  #新建一个列表,将要提取的数据放到这个新列表里
 4     with open(file="data.csv",mode="r",encoding="UTF-8") as f:
 5         reader=csv.reader(f)  #读取列表样式.reader()
 6         next(reader) #不读取第一行username,password,city
 7         for item in reader:
 8             lists.append(item)
 9     return lists
10 print(readCsvList())

运行结果如下:

[['黛西', '17166230', '中国西安'], ['Lucy', '17166230', '西安']]

Process finished with exit code 0

3-2:字典

step1:和上面的一致

step2:和上面的一致

假设我要读取:第二,三行的内容

step3:在“csv学习.py”文件中写入如下代码:

1 import csv
2 def readCsvList():
3     lists=[]
4     with open(file="data.csv", mode="r", encoding="UTF-8-sig") as f:  #注意:encoding="UTF-8-sig"
5         reader=csv.DictReader(f)  #读取字典样式.DictReader()
6         for item in reader:
7             lists.append(dict(item))
8     return lists
9 print(readCsvList())

运行结果如下:

[{'username': '黛西', 'password': '17166230', 'city': '中国西安'}, {'username': 'Lucy', 'password': '17166230', 'city': '西安'}]

Process finished with exit code 0

四、Excel

在Python中,操作Excel的文件需要使用第三方的库xlrd,需要单独安装,安装命令为:

pip3 install xlrd

step1:首先在桌面新建一个名为“data.xlsx”的Excel文件”,输入如下内容:

python usb驱动开发 python写驱动程序_json_07

 step2:在PyCharm里新建一个名为“数据驱动”的包,在这个包里新建一个python file,名叫“excel学习.py”的文件,再将step1里“data.xlsx”文件移入到“数据驱动”的包里,如下图所示:

python usb驱动开发 python写驱动程序_json_08

假设我要读取:第二,三行的内容

step3:在“excel学习.py”文件中写入如下代码:

1 import xlrd #引入xlrd包
 2 #读取行
 3 def readCsvList():
 4     lists=[]
 5     book=xlrd.open_workbook("data.xlsx") #打开Excel文件,存在book里(不用一定叫book,改成别的名字也可以,但是要记得下面一行的book也要替换)
 6     sheet=book.sheet_by_index(0)  #索引是0的是第一张表
 7     for item in range(1,sheet.nrows): #按行读取,for循环就是输出所要求的行
 8         lists.append(sheet.row_values(item)) #转换为表格形式
 9     return lists
10 print(readCsvList())
11 #读取列
12 def readCsvList():
13     lists=[]
14     book=xlrd.open_workbook("data.xlsx") 
15     sheet=book.sheet_by_index(0)  
16     for item in range(1,sheet.ncols): #按列读取
17         lists.append(sheet.col_values(item))
18     return lists
19 print(readCsvList())

运行结果如下:

[['黛西', 17166230.0, '中国西安'], ['Lucy', 17166230.0, '西安']]
[['password', 17166230.0, 17166230.0], ['city', '中国西安', '西安']]

Process finished with exit code 0