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 requestsimport pytestimport csvdef 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,错误的accessToken999900,helloworld,ask,helloworld,401,错误的accessToken6c231bd4-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 requestsimport pytestimport csvdef 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_msgtest_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
