pytest数据驱动(最简单)

目录

第一种:通过yaml文件获取数据(一维列表)

第二种:通过yaml文件获取数据(二维列表)

第三种:通过yaml文件获取数据(@pytest.fixture)

资料获取方法


第一种:通过yaml文件获取数据(一维列表)

data.yaml文件内容如下:

  • '软件测试'

  • '单元测试'

  • '自动化测试'

  • '性能测试'

  • '测试开发'

  • '测试架构师'

测试用例内容如下:

python 复制代码
@pytest.mark.parametrize('data',  yaml.load(open('data.yml', 'r')))
def test_ddt(data):

  url='https://www.baidu.com/search/query?key='
  header = {'Accept': "application/json",
      'Content-Type': "application/json; charset=utf-8",
      'Accept-Encoding': "gzip, deflate, br"}

  res=requests.get(url+data, header)
  assert res.status_code==200

第二种:通过yaml文件获取数据(二维列表)

data.yaml文件内容如下:

python 复制代码
#用例1
-
api_name: page_title
url: http://www.baidu.com/
header = {'Accept': "application/json",
     'Content-Type': "application/json; charset=utf-8",
     'Accept-Encoding': "gzip, deflate, br"}
data: {
"status_code": 200
}
python 复制代码
#用例2
-
api_name: searching
url: http://www.baidu.com/
header = {'Accept': "application/json",
     'Content-Type': "application/json; charset=utf-8",
     'Accept-Encoding': "gzip, deflate, br"}
data: {
"status_code": 200
}
python 复制代码
#用例3
-
api_name: login
url: http://www.baidu.com/
header = {'Accept': "application/json",
     'Content-Type': "application/json; charset=utf-8",
     'Accept-Encoding': "gzip, deflate, br"}
data: {
"status_code": 200
}

测试用例内容如下:

python 复制代码
@pytest.mark.parametrize('data', yaml.load(open('data.yml', 'r')))
def test_ddt(data):
       api_name = data['api_name']
  url=data['url']
  header = data['header']

  res=requests.get(url + api_name, header)
  assert   res.status_code ==data['data']['status_code']

第三种:通过yaml文件获取数据(@pytest.fixture)

python 复制代码
@pytest.fixture()
def login(request):
  name = request.param
  print(f"== 账号是:{name} ==")
  return name

data = ["pyy1", "polo"]

@pytest.mark.parametrize("login", data, indirect=True)
def test_name(login):
  print(f" 测试用例的登录账号是:{login} ")
python 复制代码
@pytest.fixture()
def logins(request):
  param = request.param
  print(f"账号是:{param['username']},密码是:{param['pwd']}")
  return param

data = [ {"username": "name1", "pwd": "pwd1"},  {"username": "name2", "pwd": "pwd2"} ]

@pytest.mark.parametrize("logins", data, indirect=True)
def test_name_pwd(logins):
  print(f"账号是:{logins['username']},密码是:{logins['pwd']}")
python 复制代码
# 多个fixture
@pytest.fixture(scope="module")
def input_user(request):
  user = request.param
  print("登录账户:%s" % user)
  return user

@pytest.fixture(scope="module")
def input_psw(request):
  psw = request.param
  print("登录密码:%s" % psw)
  return psw

data = [("name1", "pwd1"),  ("name2", "pwd2")]

@pytest.mark.parametrize("input_user,input_psw", data, indirect=True)
def test_more_fixture(input_user, input_psw):
  print("fixture返回的内容:", input_user, input_psw)



name = ["name1", "name2"]
pwd = ["pwd1", "pwd2"]

@pytest.mark.parametrize("input_user", name, indirect=True)
@pytest.mark.parametrize("input_psw", pwd, indirect=True)
def test_more_fixture(input_user, input_psw):
  print("fixture返回的内容:", input_user, input_psw)

资料获取方法

【留言777】

各位想获取源码等教程资料的朋友请 点赞 + 评论 + 收藏 ,三连!

三连之后我会在评论区挨个私信发给你们~

相关推荐
c萱2 天前
软件测试错题笔记
软件测试·数据库·笔记·测试工具·oracle·测试用例
川石教育2 天前
软件测试中的Bug知识总结
软件测试·bug·压力测试·缺陷管理·bug分类
利以檀本人(梦泽不忙)3 天前
#T1359. 围成面积
c++·程序人生·算法
jingling5553 天前
【高级】系统架构师 | 信息系统战略规划、EAI 与新技术
程序人生·职场和发展·系统架构·学习方法·程序员创富
序属秋秋秋3 天前
我的创作纪念日——《惊变365天》
经验分享·学习·程序人生·纪念日·生活·大学生
蓑衣客VS索尼克3 天前
第三章:生活重构:当程序员不再只是“码农“
学习·程序人生·重构·生活
程序员雨果4 天前
外包干了3天,技术退步明显.......
软件测试·面试
武子康4 天前
66AI-调查研究-68-具身智能 应用全景:家庭、工业、医疗、交通到虚拟交互的未来趋势
人工智能·程序人生·ai·职场和发展·交互·个人开发·具身智能
程序员小远4 天前
基于jmeter+perfmon的稳定性测试记录
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·测试用例
swanwei4 天前
技术人员突围之路(15年技术管理经验总结)之十
学习·程序人生·生活·求职招聘·交友