1.url参数化 用format实现参数化
def test_get_path():
# 使用.format进行字符串格式化
r = requests.request("GET","https://qyapi.weixin.qq.com/cgi-bin/{pageNum}/{pageSize}".format(pageNum=1,pageSize=10))
2.写在类里面调用,url=f'xxx{self.token}'里面
class TestDepartment:
#【冒烟】合法参数可以成功创建部门
# 测试过程中,常见的接口依赖
def setup_class(self):
# 对于依赖接口的描述
ID = "xxxx"
secret = "xxxx"
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={ID}&corpsecret={secret}"
r = requests.get(url)
# 如果使用r.json() 响应内容即为python的字典结构
self.token = r.json()["access_token"]
@pytest.mark.parametrize("name", ["", "2", "2"*32, "2"*33])
def test_create_department(self, name):
# 添加token
## 对于接口的描述
## 对于业务的描述
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={self.token}"
data = {
"name": name,
"name_en": name,
"parentid": 1,
}
r = requests.post(url, json=data)
# 断言
assert r.json()["error_code"] == 0
3.预期结果参数化
resultList= [
[1,2,3],
[4,5,9],
['he','llo','hello']
]
@pytest.mark.parametrize(
'a,b,result',
resultList
)
def test_add(a,b,result): #a,b,result接收装饰器传入的数据
print("{0}-{1}->{2}".format(a,b,result))
assert add(a,b)==result