csv 文件读写
https://docs.python.org/zh-cn/3/library/csv.html
可以使用csv模块进行csv文件数据的读写。 csv模块是python内置的,所以不需要安装,可以直接使用。
csv文件写入
csv_demo.py
# 导入csv模块
import csv
# 打开csv文件,权限设置为写入 设置不要换行
with open('data.csv',mode='w',encoding='utf8',newline='') as file:
# 使用csv模块往文件中写入内容
wr = csv.writer(file)
# 写入一行内容 使用列表格式数据
wr.writerow(["用户名","密码","期望状态码"])
for i in range(10):
wr.writerow([f'test{i}',"123456",200])
csv文件读取
csv_demo.py
# 读取csv文件 文件权限为 r 读取
with open('data.csv',mode='r',encoding='utf8') as file:
# 读取文件
rd = csv.reader(file)
# 循环读取
for line in rd:
print(line)
接口测试CSV数据驱动
也可以将测试数据放到csv文件中,通过读取csv文件的内容实现接口的自动化。
准备测试数据
data.csv
用户名,密码,期望状态码,期望的message
,123456,510,登录名不能为空
1321111,123456,500,请输入正确的手机号!
13213331333,,510,密码不能为空
,,510,密码不能为空
13213331333,123456,500,登录失败!
读取csv文件进行接口自动化
test_csv_data.py
"""
读取csv文件中的内容,进行数据驱动接口测试
"""
import requests
import pytest
import csv
def read_csv():
testdata = []
with open('data.csv',mode='r',encoding='utf8') as file:
rd = csv.reader(file)
# 去掉第一行数据
next(rd)
for line in rd:
testdata.append(tuple(line))
return testdata
# 读取csv文件中的数据
testdata = read_csv()
print(testdata)
@pytest.mark.parametrize("username,passwd,excpet_code,excpet_msg",testdata)
def test_login(username,passwd,excpet_code,excpet_msg):
url = "http://49.233.108.117:28019/api/v1/user/login"
body = {
"loginName": username,
"passwordMd5": passwd
}
# 发送请求
r = requests.post(url,json=body)
print(r.json())
# 添加断言 因为从csv文件中读取出来的数字,会自动被转换为字符串
assert r.json()["resultCode"] == int(excpet_code) # 将csv文件中的字符串转为数字
assert r.json()["message"] == excpet_msg
运行
pytest -s -v test_csv_data.py
总结
从csv文件中读取数据,使用csv模块,csv文件中读取的数据默认为字符串类型,在加断言的时候,针对状态码进行断言,断言的结果中需要将字符串再转换为数字。
csv数据驱动参数化,本质上还是从 将数据从文件中读取出来,将数据转换为列表格式, 使用pytest的参数化功能进行自动化。
csv参数化
topic.csv
token,title,tab,content,期望的状态码,期望的错误提示信息
,helloworld,ask,helloworld,401,错误的accessToken
999900,helloworld,ask,helloworld,401,错误的accessToken
6c231bd4-1180-466e-9c01-dfc83eaddbc8,,ask,helloworld,400,标题不能为空
6c231bd4-1180-466e-9c01-dfc83eaddbc8,helloworld,,helloworkld,400,必须选择一个版块
6c231bd4-1180-466e-9c01-dfc83eaddbc8,helloworld,ask,,400,内容不可为空
test_csv_data.py
"""
读取csv文件中的内容,进行数据驱动接口测试
"""
import requests
import pytest
import csv
def read_csv(filename):
testdata = []
with open(filename,mode='r',encoding='utf8') as file:
rd = csv.reader(file)
# 去掉第一行数据
next(rd)
for line in rd:
testdata.append(tuple(line))
return testdata
# 读取csv文件中的数据
testdata = read_csv('data.csv')
print(testdata)
@pytest.mark.parametrize("username,passwd,excpet_code,excpet_msg",testdata)
def test_login(username,passwd,excpet_code,excpet_msg):
url = "http://49.233.108.117:28019/api/v1/user/login"
body = {
"loginName": username,
"passwordMd5": passwd
}
# 发送请求
r = requests.post(url,json=body)
print(r.json())
# 添加断言 因为从csv文件中读取出来的数字,会自动被转换为字符串
assert r.json()["resultCode"] == int(excpet_code) # 将csv文件中的字符串转为数字
assert r.json()["message"] == excpet_msg
test_topic_data = read_csv('topic.csv')
@pytest.mark.parametrize('token,title,tab,content,expect_code,expect_msg',test_topic_data)
def test_topics(token,title,tab,content,expect_code,expect_msg):
url = "http://47.100.175.62:3000/api/v1/topics"
body = {
"accesstoken":token,
"title":title,
"tab":tab,
"content":content
}
r = requests.post(url,json=body)
print(r.status_code,r.json())
# 状态码断言
assert r.status_code == int(expect_code)
# 服务器返回结果断言
assert r.json()["error_msg"] == expect_msg