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】

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

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

相关推荐
测试者家园14 小时前
DevOps 到底改变了测试什么?
运维·自动化测试·软件测试·devops·持续测试·智能化测试·软件测试和开发
程序员小远16 小时前
Appium-移动端自动测试框架详解
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
swanwei20 小时前
2025年11月22-23日互联网技术热点TOP3及影响分析(AI增量训练框架开源)
网络·人工智能·程序人生·安全·百度
淼_@淼1 天前
pytest-数据驱动
pytest
yesyesido1 天前
AI电脑配置推荐工具:智能匹配需求,一键生成个性化硬件方案
人工智能·科技·学习·程序人生·生活
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 27--二次封装方法--优化断言结果
python·学习·测试工具·pytest