单一接口多种数据验证

1、yaml 文件的内容

#用户登录成功
url: http://127.0.0.1:5000/login/
method: post
data:
  username: "admin"
  password: admin
  age: 18
  sex: "男"
expect: admin

---
url: http://127.0.0.1:5000/login/
method: post
data:
  password: 123456
  age: 18
  sex: "男"
expect: 您的用户验证错误

---
url: http://127.0.0.1:5000/login/
method: post
data:
  username: "admin"
  age: 18
  sex: "男"
expect: 账户密码不能为空

---

url: http://127.0.0.1:5000/login/
method: post
data:
  username: "admin"
  password: admin
  age: 18
  sex: sd
expect: 性别只能是男或女

2、验证接口脚本

# 1、读取 yaml 文件内容
import yaml

def readYaml(self):
	with open('login.yaml','r',encoding='utf-8') as f:
		return yaml.safe_load_all(f)

# 2、验证接口
import pytest
import json
import requests

@pytest.mark.parametrize('datas',readYaml())
def test_login(datas):
	r = requests.post(url=datas['url'],
	             json=datas['data'])
	assert datas['expect'] in json.dumps(r.json(),ensure_ascii=False)

if __name__ == '__main__':
    pytest.main(["-s","-v","test_login.py"])

  

原文地址:https://www.cnblogs.com/siyu0123/p/12844542.html