数据驱动测试实例

1、读取txt文件

# 读取数据文件
# 打开
user_file = open('user_info.txt', 'r')
# 读取多行
lines = user_file.readlines()
# 关闭文件
user_file.close()
# 遍历拆分
for line in lines:
	username = line.split(',')[0]
	password = line.split(',')[1]
	print(username, password)

# user_info.txt
zhangsan,123
lisi,456
wangwu,789

2、读取CSV文件:先用Excel写,在保存为CSV格式

# 读取CSV格式
import csv
# 读取本地CSV文件
data = csv.reader(open('11.csv', 'r'))
# 循环输出每一行信息
for user in data:
	print(user)



# 11.csv
test,[email protected],23,man
test2,[email protected],34,woman
test3,[email protected],22,man

3、读取xml文件

# 操作xml文件
from xml.dom import minidom

# 打开xml文档
dom = minidom.parse('user_info.xml')

# 得到文档元素对象。唯一根元素
root = dom.documentElement

# 打印节点名,node和tag一样
print(root.nodeName)
print(root.tagName)
# 打印节点值,只对文本节点有效
print(root.nodeValue)

# 得到任意标签名
browser = root.getElementsByTagName('browser')
print(browser[0].tagName)
print(browser[0].nodeName)

# 得到属性值
logins = root.getElementsByTagName('login')
username = logins[0].getAttribute('username')
print(username)

# 得到标签对里的值
province = root.getElementsByTagName('province')
print(province[0].firstChild.data)

user_info.xml



	
		Window
		Firefox
		http://www.baidu.com
		
		
	
	
		北京
		广东
			深圳
			珠海
		浙江
			杭州
	

 

你可能感兴趣的:(自动化测试)