csv 文件读写

https://docs.python.org/zh-cn/3/library/csv.html
可以使用csv模块进行csv文件数据的读写。 csv模块是python内置的,所以不需要安装,可以直接使用。

csv文件写入

csv_demo.py

  1. # 导入csv模块
  2. import csv
  3. # 打开csv文件,权限设置为写入 设置不要换行
  4. with open('data.csv',mode='w',encoding='utf8',newline='') as file:
  5. # 使用csv模块往文件中写入内容
  6. wr = csv.writer(file)
  7. # 写入一行内容 使用列表格式数据
  8. wr.writerow(["用户名","密码","期望状态码"])
  9. for i in range(10):
  10. wr.writerow([f'test{i}',"123456",200])

csv文件读取

csv_demo.py

  1. # 读取csv文件 文件权限为 r 读取
  2. with open('data.csv',mode='r',encoding='utf8') as file:
  3. # 读取文件
  4. rd = csv.reader(file)
  5. # 循环读取
  6. for line in rd:
  7. print(line)

接口测试CSV数据驱动

也可以将测试数据放到csv文件中,通过读取csv文件的内容实现接口的自动化。

准备测试数据

data.csv

  1. 用户名,密码,期望状态码,期望的message
  2. ,123456,510,登录名不能为空
  3. 1321111,123456,500,请输入正确的手机号!
  4. 13213331333,,510,密码不能为空
  5. ,,510,密码不能为空
  6. 13213331333,123456,500,登录失败!

读取csv文件进行接口自动化

test_csv_data.py

  1. """
  2. 读取csv文件中的内容,进行数据驱动接口测试
  3. """
  4. import requests
  5. import pytest
  6. import csv
  7. def read_csv():
  8. testdata = []
  9. with open('data.csv',mode='r',encoding='utf8') as file:
  10. rd = csv.reader(file)
  11. # 去掉第一行数据
  12. next(rd)
  13. for line in rd:
  14. testdata.append(tuple(line))
  15. return testdata
  16. # 读取csv文件中的数据
  17. testdata = read_csv()
  18. print(testdata)
  19. @pytest.mark.parametrize("username,passwd,excpet_code,excpet_msg",testdata)
  20. def test_login(username,passwd,excpet_code,excpet_msg):
  21. url = "http://49.233.108.117:28019/api/v1/user/login"
  22. body = {
  23. "loginName": username,
  24. "passwordMd5": passwd
  25. }
  26. # 发送请求
  27. r = requests.post(url,json=body)
  28. print(r.json())
  29. # 添加断言 因为从csv文件中读取出来的数字,会自动被转换为字符串
  30. assert r.json()["resultCode"] == int(excpet_code) # 将csv文件中的字符串转为数字
  31. assert r.json()["message"] == excpet_msg

运行

  1. pytest -s -v test_csv_data.py

总结

从csv文件中读取数据,使用csv模块,csv文件中读取的数据默认为字符串类型,在加断言的时候,针对状态码进行断言,断言的结果中需要将字符串再转换为数字。

csv数据驱动参数化,本质上还是从 将数据从文件中读取出来,将数据转换为列表格式, 使用pytest的参数化功能进行自动化。


csv参数化

topic.csv

  1. token,title,tab,content,期望的状态码,期望的错误提示信息
  2. ,helloworld,ask,helloworld,401,错误的accessToken
  3. 999900,helloworld,ask,helloworld,401,错误的accessToken
  4. 6c231bd4-1180-466e-9c01-dfc83eaddbc8,,ask,helloworld,400,标题不能为空
  5. 6c231bd4-1180-466e-9c01-dfc83eaddbc8,helloworld,,helloworkld,400,必须选择一个版块
  6. 6c231bd4-1180-466e-9c01-dfc83eaddbc8,helloworld,ask,,400,内容不可为空

test_csv_data.py

  1. """
  2. 读取csv文件中的内容,进行数据驱动接口测试
  3. """
  4. import requests
  5. import pytest
  6. import csv
  7. def read_csv(filename):
  8. testdata = []
  9. with open(filename,mode='r',encoding='utf8') as file:
  10. rd = csv.reader(file)
  11. # 去掉第一行数据
  12. next(rd)
  13. for line in rd:
  14. testdata.append(tuple(line))
  15. return testdata
  16. # 读取csv文件中的数据
  17. testdata = read_csv('data.csv')
  18. print(testdata)
  19. @pytest.mark.parametrize("username,passwd,excpet_code,excpet_msg",testdata)
  20. def test_login(username,passwd,excpet_code,excpet_msg):
  21. url = "http://49.233.108.117:28019/api/v1/user/login"
  22. body = {
  23. "loginName": username,
  24. "passwordMd5": passwd
  25. }
  26. # 发送请求
  27. r = requests.post(url,json=body)
  28. print(r.json())
  29. # 添加断言 因为从csv文件中读取出来的数字,会自动被转换为字符串
  30. assert r.json()["resultCode"] == int(excpet_code) # 将csv文件中的字符串转为数字
  31. assert r.json()["message"] == excpet_msg
  32. test_topic_data = read_csv('topic.csv')
  33. @pytest.mark.parametrize('token,title,tab,content,expect_code,expect_msg',test_topic_data)
  34. def test_topics(token,title,tab,content,expect_code,expect_msg):
  35. url = "http://47.100.175.62:3000/api/v1/topics"
  36. body = {
  37. "accesstoken":token,
  38. "title":title,
  39. "tab":tab,
  40. "content":content
  41. }
  42. r = requests.post(url,json=body)
  43. print(r.status_code,r.json())
  44. # 状态码断言
  45. assert r.status_code == int(expect_code)
  46. # 服务器返回结果断言
  47. assert r.json()["error_msg"] == expect_msg