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】

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

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

相关推荐
yaoh.wang1 小时前
力扣(LeetCode) 67: 二进制求和 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
小年糕是糕手1 小时前
【C++】string类(二)
开发语言·数据结构·c++·程序人生·算法·leetcode·数字货币
yaoh.wang1 小时前
力扣(LeetCode) 58: 最后一个单词的长度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
郝学胜-神的一滴1 小时前
使用Qt OpenGL开发俄罗斯方块:从零到一实现经典游戏
c++·qt·程序人生·游戏·设计模式·系统架构·图形渲染
玄斎12 小时前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
CeshirenTester14 小时前
Playwright元素定位详解:8种定位策略实战指南
人工智能·功能测试·程序人生·单元测试·自动化
测试老哥1 天前
UI自动化测试—Jenkins配置优化
自动化测试·软件测试·python·测试工具·ui·jenkins·测试用例
测试老哥1 天前
2026软件测试面试大全(含答案+文档)
自动化测试·软件测试·python·测试工具·面试·职场和发展·测试用例
yaoh.wang1 天前
力扣(LeetCode) 28: 找出字符串中第一个匹配项的下标 - 解法思
python·程序人生·算法·leetcode·面试·职场和发展·跳槽